No error at all, why?

I am testing this code, but why is there a mistake at all?

#include <stdio.h>

int main()
{

    int a = 1025;
    int *p;
    p = &a;

    // now I declare a char variable , 
    char *p0;
    p0 = (char*) p; // type casting
    printf("", sizeof(char));

    // is %d correct here ?????
    printf("Address = %d, value = %d\n", p0, *p0);

}

My question is: Is it right here %d? since the %dwhole is not for the symbol, why is there a mistake at all?

+4
source share
2 answers

In your case

 p0 = (char*) p;

because it char *can be used to access any other types. Related, citing C11, chapter §6.3.2.3

[...] When a pointer to an object is converted to a pointer to a character type, the result points to the least significant address byte of the object. Successive increments of the result, up to the size of the object, prints pointers to the remaining bytes of the object.

However, in the case of

    printf("Address = %d, value = %d\n", p0, *p0);

undefined , (p0) %d ( "" ). %p void *, -

   printf("Address = %p, value = %d\n", (void *)p0, *p0);

,

, ?

, . .:)

+4

undefined, p0 char*, printf("Address = %d, value = %d\n", p0, *p0) %d .

+1

Source: https://habr.com/ru/post/1672533/


All Articles