Currently I'm following this pdf on C pointers. The code below was meant as a demonstration but confused me beyond belief. Firstly, my understanding of pointers to arrays is that they are initialized in heap memory with the memory address of the first element in the array hence why: ptr = &my_array[0]; /* and */ ptr = my_array;
are the same thing. My first issue with this code is that he created a pointer that doesn't point to a memory address. I thought pointer initialization would usually involve the & and the value of pA would be the memory address of the first element of the array, but when he prints the pointer to the screen THE WHOLE STRING PRINTS OUT. Why? On top of that, when I put another * on pA it prints the first character of the array. Why? On top of that, he uses ++ on *pA and *pB which makes no sense to me because the output shows that *pA is the first character in the array not a memory address.
Sorry this concept has been tough for me to wrap my head around. Is the PDF just bad at explaining pointers or am I grossly misunderstanding something?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char strA[80] = "A string to be used for demonstration purposes";
char strB[80];
int main(int argc, char *argv[]){
char *pA; /* a pointer to type character */
char *pB; /* another pointer to type character */
puts(strA); /* show string A */
pA = strA; /* point pA at string A */
puts(pA); /* show what pA is pointing to */
pB = strB; /* point pB at string B */
putchar('\n'); /* move down one line on the screen */
printf("\n%c\n", *pA);
while(*pA != '\0') /* line A (see text) */
{
*pB++ = *pA++; /* line B (see text) */
}
*pB = '\0'; /* line C (see text) */
puts(strB); /* show strB on screen */
return 0;
}
Output:
A string to be used for demonstration purposes
A string to be used for demonstration purposes
A
A string to be used for demonstration purposes
EDIT: Thanks for the help everyone :), sorry about the noobie question. I read Programming in C by Stephen G. Kochan and its amazing section on pointers helped a lot.
char strA[80] = "A string to be used for demonstration purposes";
This makes an 80 character long space in the global section of the executable - it isn't dynamically allocated so it doesn't need to be deallocated. If strA is used as a pointer it is equivalent to &strA[0]
as you expect.
Those 80 characters are filled with the string on the right side of the equals - including the terminating character at the end of the string. That terminating character is really important because that's how you find the end of the printable data in the array.
puts(strA); /* show string A */
When you puts(strA)
, that function prints EVERY character from the address passed in up to that terminating character - there is a loop inside that function.
pA = strA; /* point pA at string A */
puts(pA); /* show what pA is pointing to */
Then pA
is filled with the value of &strA[0]
and then when puts(pA);
is called, the function receives the same address it received the last time so it does the same thing as last time: it prints EVERY character from the address passed in up to that terminating character.
printf("\n%c\n", *pA);
while(*pA != '\0') /* line A (see text) */
The *
character gives you the contents at the location of the pointer - so *strA
gives the same thing as strA[0]
- the first character of the string. And *pA
is the same as *strA
because they both point to the same address.
*pB++ = *pA++; /* line B (see text) */
The reason *pA++
confuses you is because you don't know whether the *
or the ++
is done first. What happens is: pA++
increments the pointer and returns the original value it had. Then the *
takes that original value of the pointer and gives its content - the character where pA originally pointed.