0

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);

1 Answer 1

1

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?

1
  • I had watched week 4 lectures. Will watch again once more time.
    – d0020002
    Commented Oct 9, 2017 at 0:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .