-1

I am a new learner for c. And I met with this ERROR.

int *p;
*p=6;//ERROR
int a=6;
*p=a;//Right

Could you please explain this situation? Thank you very much!

chd_li
  • 13
  • 1

1 Answers1

2

Both example are incorrect.

Before you can dereference a pointer, you need to make it point someplace. For example:

int *p;
int a;
p = &a;
a = 5;
printf("*p=%d\n", *p); // prints *p=5
a = 6;
printf("*p=%d\n", *p); // prints *p=6
dbush
  • 205,898
  • 23
  • 218
  • 273