P
Prathamesh Kulkarni
#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 *.
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 *.