TheLogicGuy TheLogicGuy - 5 days ago 6
C Question

Differences between printing a pointer with and without & in C99

Consider the following code:

const char* text = "hi";
printf("%s\n",text);
printf("%p\n", &text);
printf("%p\n", text);


From where does every
printf
takes the value it prints?

What are the differences?

Answer

So let's remember that a pointer is a memory address. text is, as I suspect you know, a pointer to the first (or 0th, depending on how you like to think about it) block of a char array. The & is the "address" operator, which returns the memory address (that is, a pointer to...) whatever comes after it. Since the literal pointer value of text is itself some kind of data, it has to be stored somewhere in memory. So &text returns the address of where the value of text is stored. Like this: POINTERS!!!

Comments