I've been studying C for a few months at university now, but I missed a lecture about pointers, so I tried to make up for it by studying it online and I thought I got it - but something I just stumbled upon is extremely irritating for me.
I know that pointers hold nothing more than the address they are pointing to - for example, if I understood everything correctly so far, I have:
int *pointer;
int number = 30;
pointer = &number;
printf("Number at location: %d", *pointer);
And this works fine, as it should. I assign the adress of the variable number to pointer and then print it in the end by dereferencing pointer and getting the actual value from the adress. What irritates me though are char pointers.
I've read up on string arrays/pointers, so I tried a few things, when I noticed that something strange (for my eyes at least) happened, with int pointers too:
char* pointer;
char array[] = "Dingleberry";
pointer = array;
printf("%s\n", pointer);
return 0;
I know that I am not directly assigning the adress, but if I remember correctly, with arrays, that's not necessary in conjunction with pointers - anyway - this code here works as expected, it prints out "Dingleberry". My problem now is... why? Shouldn't the pointer, without dereferencing, only hold the address of the value? If I would dereference here, the program crashes, it does show the address if I use the & though.
I'm not getting any warnings whatsoever when compiling. Also, shouldn't it work if I were to use:
printf("%c", pointer);
to only get one letter? (I mean, trying this does show a warning - but I'm interested in getting better and ruling out most likely stupid misconceptions on my part.)