I’m doing “Learn C the hard way” for self-study coming from knowing a bit of Python. I have read several tutorials, but I can’t get my head around how pointers and assignment works. I understand that if you dereference the pointer, you can directly give it a value as in:
int *anint = 42;
But what about specifically referencing the memory location of an already-created variable?
Specifically, I tried:
char *pointer_to_strlit;
char *strlit = "some stuff";
pointer_to_strlit = &strlit;
Why does the following cause a segfault after I do this:
printf("I print strlit: %s\nI print it again by pointing to it: %s\nI print where the pointer is pointing: %p\n", strlit, *pointer_to_strlit, pointer_to_strlit);
The types in C seem really hard tell how they will behave and how to use pointers to reference the specific types. Is there a clear guide that specifically outlines the syntax for pointing to each different datatype (char, *char, *char[], int, struct, void, null, functions, etc.)? Even a list of steps that would help me understand the rule set would be useful.
Hang in there! Pointers will make sense after more practice. But when in doubt, try to reason about what each value means. Using pen and paper to try to draw each byte in memory really helps.
char *pointer_to_strlit;– here you declare a pointer to a character. As you probably already know, a string in C is represented by a pointer to the first character of that string. The string is expected to be null-terminated. This means that eventually there should be an ASCII0character indicating that the string has ended.char *strlit = "some stuff";– your program’s memory will contain characters for this string (11 characters to be exact — 10 for the text you see, and 1 for the null terminator). Here you declare another pointer, this time pointing to first character “s” from that string.pointer_to_strlit = &strlit;– this sets the value ofpointer_to_strlitto the address of the pointerstrlit. This is probably not what you want here.If things get confusing, try to think of each pointer as a plain old number — that’s essentially what a pointer is, a huge number representing an address in memory. Let’s look at the above again:
char *pointer_to_strlit;– Here the value ofpointer_to_strlitis undefined since you didn’t set it yet.char *strlit = "some stuff";– Let’s say the address of the first “s” is1234500. The value ofstrlitwill be that number,1234500.pointer_to_strlit = &strlit;– But what is the address ofstrlititself? It’s some other value, let’s say1234600. The value ofpointer_to_strlitwill now be1234600.Try to print
pointer_to_strlitas a%snow, and your program will crash — at the address1234600is not the first character of a string, but another number — one of the bytes of the huge number, the pointer. The code will try to traverse what it thinks is a string to look for the null-terminator, eventually crashing when it reaches inaccessible memory.