I'm new to pointers, and I'm running some test code. I am aware that a segmentation fault means that I'm trying to access some piece of memory that I shouldn't be; however, I don't understand what's wrong with my line of thinking here.
int main(void) {
int *ptr;
*ptr = 400;
printf("%d\n", *ptr);
return 0;
}
The function compiles just fine. From my understanding, when ptr is declared, it has an arbitrary memory address. Then the second line puts 400 as a reference to that memory address. Finally, the printf() line should return 400, but it's giving me a core dump.
Can someone please clarify? I've looked at several pointer examples, and I thought I was getting the hang of it, until I tried this.