I am trying to test my understanding of pointers and wrote the following piece of code:
#include <stdio.h>
int main(){
char array_of_chars[]="array of chars";
char *pointer_to_a_char="pointer to a char";
return 0;
}
My justification for why the 2 lines of code are equivalent ways to define a string are that:
The first creates an array of indefinite size (limited by the memory available in the stack?) which stores variables of type char .
The second creates a pointer to a variable of type char, which by the * notation we are then funneling through to where that memory address points to in the RAM and are then writing, from that point, our string.
The above compiles without error.
This new code however, gives a warning.
New code:
#include <stdio.h>
int main(){
int myint = 5;
int *pointer_to_an_int = 5;
return 0;
}
warning: initialization makes pointer from integer without a cast [-Wint-conversion]
int *pointer_to_an_int = 5;
I just wanted to know why we get a warning in the second case, but not in the first.
I have a feelings its something to do with the fact that in the first case we are defining an array, which is a memory address but in the second case we are defining a single variable which is different? I don't know the reason for the warning exactly and was hoping someone can explain.