Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > incompatible pointer assignment

Reply
Thread Tools

incompatible pointer assignment

 
 
Prathamesh Kulkarni
Guest
Posts: n/a
 
      12-10-2012
#include <stdio.h>

int
main(void)
{
char *p;
float a = 1.2;

p = &a;
printf("%p\n", p);
return 0;
}

when I compile this code with
gcc -std=c11, I get
warning: assignment from incompatible
pointer type [enabled by default]

I am not able to understand why.
From n1570 6.3.2.3 7th point (page 74):
When a pointer to an object is converted
to a pointer to a character type,the result
points to the lowest addressed byte of the
object.Successive increments of the result,
up to the size of the object, yield pointers
to the remaining bytes of the object

So I guess, that conversion of float * to char *
is defined in n1570 or am I interpreting the
standard incorrectly ?

I know that the above code is incorrect
in C99. From c99 rationale v5.10 6.3.2.3 (page 49):
It is invalid to convert a pointer to
an object of any type to a pointer
to an object of a different type without an
explicit cast.
So it would be invalid to assign &a to p without
casting &a to char *.
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      12-10-2012
Prathamesh Kulkarni <> writes:

> #include <stdio.h>
>
> int
> main(void)
> {
> char *p;
> float a = 1.2;
>
> p = &a;
> printf("%p\n", p);
> return 0;
> }
>
> when I compile this code with
> gcc -std=c11, I get
> warning: assignment from incompatible
> pointer type [enabled by default]
>
> I am not able to understand why.
> From n1570 6.3.2.3 7th point (page 74):
> When a pointer to an object is converted
> to a pointer to a character type,the result
> points to the lowest addressed byte of the
> object.Successive increments of the result,
> up to the size of the object, yield pointers
> to the remaining bytes of the object
>
> So I guess, that conversion of float * to char *
> is defined in n1570 or am I interpreting the
> standard incorrectly ?


Not quote. The part you quote defines the conversion but not how it
happens. The rules for assignment will tell you that the pointer types
must be (almost) compatible (you can have some differing qualifiers) so
a compiler must issue a diagnostic. As you say below, the conversion
*is* defined, but you must ask for it:

p = (char *)&a;

> I know that the above code is incorrect
> in C99. From c99 rationale v5.10 6.3.2.3 (page 49):
> It is invalid to convert a pointer to
> an object of any type to a pointer
> to an object of a different type without an
> explicit cast.
> So it would be invalid to assign &a to p without
> casting &a to char *.


Exactly. The conversion is defined but it does not happen
automatically.

--
Ben.
 
Reply With Quote
 
 
 
 
Bart van Ingen Schenau
Guest
Posts: n/a
 
      12-10-2012
On Mon, 10 Dec 2012 03:16:05 -0800, Prathamesh Kulkarni wrote:

> #include <stdio.h>
>
> int
> main(void)
> {
> char *p;
> float a = 1.2;
>
> p = &a;
> printf("%p\n", p);
> return 0;
> }
>
> when I compile this code with
> gcc -std=c11, I get
> warning: assignment from incompatible pointer type [enabled by default]
>
> I am not able to understand why.
> From n1570 6.3.2.3 7th point (page 74): When a pointer to an object is
> converted to a pointer to a character type,the result points to the
> lowest addressed byte of the object.Successive increments of the result,
> up to the size of the object, yield pointers to the remaining bytes of
> the object
>
> So I guess, that conversion of float * to char * is defined in n1570 or
> am I interpreting the standard incorrectly ?


You are interpreting the standard incorrectly (or rather, incompletely).
Clause 6.3 governs the results of both implicit and explicit conversions
(the latter are better known as type-casts), but it does not state which
of the conversions can be performed implicitly and which must be done
explicitly.

If you look in clause 6.5.4 (Cast operators), you will find the following
under the header 'constraints':
<quote>
3 Conversions that involve pointers, other than where permitted by the
constraints of 6.5.16.1, shall be specified by means of an explicit cast.
</quote>
Section 6.5.16.1 does not list the conversion to char* as being permitted
without a cast, so your program needs a cast to perform the conversion.

>
> I know that the above code is incorrect in C99. From c99 rationale v5.10
> 6.3.2.3 (page 49): It is invalid to convert a pointer to an object of
> any type to a pointer
> to an object of a different type without an explicit cast.
> So it would be invalid to assign &a to p without casting &a to char *.


Actually, there are no significant changes between C99 and C11 in this
area. At most some cleaning up of terminology.

Bart v Ingen Schenau
 
Reply With Quote
 
Prathamesh Kulkarni
Guest
Posts: n/a
 
      12-10-2012
consider,
char *p; float a;
p = (char *) &a;

So n1570 6.3.2.3 7th point
states what address p would point to
after the assignment p = (char *) &a ?
I am sorry for asking dumb questions,
have never looked through the standard
before.

 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      12-10-2012
On 12/10/2012 8:24 AM, Prathamesh Kulkarni wrote:
> consider,
> char *p; float a;
> p = (char *) &a;
>
> So n1570 6.3.2.3 7th point
> states what address p would point to
> after the assignment p = (char *) &a ?


Yes: `p' will point at "the lowest addressed byte" of `a'.
The bytes that make up the representation of `a' are `p[0]',
`p[1]',...,`p[sizeof(float)-1]'.

Deciding what each of those bytes means is trickier. Each
implementation defines its own representations for the various
C data types, and they don't all use the same encoding. If you
set `a = 3.14f;' and then print `*p' on different machines, you
may well get different outputs.

> I am sorry for asking dumb questions,
> have never looked through the standard
> before.


A question isn't dumb until it's asked twice.

--
Eric Sosman
d
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      12-10-2012
On 12/10/2012 08:24 AM, Prathamesh Kulkarni wrote:
> consider,
> char *p; float a;
> p = (char *) &a;
>
> So n1570 6.3.2.3 7th point
> states what address p would point to
> after the assignment p = (char *) &a ?


Yes.

> I am sorry for asking dumb questions,
> have never looked through the standard
> before.


There's an old saying "there's no such thing as a dumb question", but
I've seen counterexamples in this very newsgroup. Your question isn't
one of those counterexamples.

One of the most common kinds of dumb questions posted here is the kind
that says "what's wrong with my program?" without containing any of the
code from the program. I can understand people giving us an irrelevant
piece of code, if they're sufficiently confused about what would be
relevant. What I don't understand is how the people who provide no code
at all expect us to diagnose the problem (it can be done, in rare cases,
but expecting it to be possible isn't reasonable).
--
James Kuyper
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-10-2012
Bart van Ingen Schenau <> writes:
[...]
> You are interpreting the standard incorrectly (or rather, incompletely).
> Clause 6.3 governs the results of both implicit and explicit conversions
> (the latter are better known as type-casts), but it does not state which
> of the conversions can be performed implicitly and which must be done
> explicitly.

[...]

And "type-casts" are better known as "casts".

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      12-10-2012
Eric Sosman <> writes:
> On 12/10/2012 8:24 AM, Prathamesh Kulkarni wrote:
>> consider,
>> char *p; float a;
>> p = (char *) &a;
>>
>> So n1570 6.3.2.3 7th point
>> states what address p would point to
>> after the assignment p = (char *) &a ?

>
> Yes: `p' will point at "the lowest addressed byte" of `a'.
> The bytes that make up the representation of `a' are `p[0]',
> `p[1]',...,`p[sizeof(float)-1]'.
>
> Deciding what each of those bytes means is trickier. Each
> implementation defines its own representations for the various
> C data types, and they don't all use the same encoding. If you
> set `a = 3.14f;' and then print `*p' on different machines, you
> may well get different outputs.


And if you want to examine the bytes that make up the representation of
some object, it's better to use unsigned char rather than plain char.
(Plain char may be either signed or unsigned, depending on the
implementation.)

[...]

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
assignment from incompatible pointer type Bart Vandewoestyne C Programming 2 04-27-2010 02:00 PM
assignment from incompatible pointer type Olaf \El Blanco\ C Programming 1 06-10-2006 04:48 PM
Pointer incompatible type assignment to character. gk245 C Programming 10 04-22-2006 05:37 AM
incompatible pointer assignment ? zmbdcqnrdfetnws C Programming 8 12-06-2003 04:52 AM
Re: malloc & incompatible types in assignment Dimitris Mandalidis C Programming 4 08-31-2003 02:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57