Why this code not is not compiling
int *pkk = 5646; printf("%d\n", *pkk); printf("%d\n", pkk);
but this works perfectly
char *sp = "Hello"; printf("%s\n",sp); printf("%c", *sp);
CS50 Stack Exchange is a question and answer site for students of Harvard University's CS50. It only takes a minute to sign up.Sign up to join this community
int * is a pointer to an int, so you'll get two errors there:
error: incompatible integer to pointer conversion initializing 'int *' with an expression of type 'int' [-Werror,-Wint-conversion] int *pkk = 5646; ^ ~~~~ ptr.c:6:18: error: format specifies type 'int' but the argument has type 'int *' [-Werror,-Wformat] printf("%d\n", pkk); ~~ ^~~
The first is saying that you can't initialize a pointer with an integer value. The second is saying that you can't print a pointer type using the
"%d" format specifier, as that is for printing an integer.
Your char * example works fine because of how C defines a char pointer.
Pointers are covered in Weeks 4 and 5. Have you already completed those?