开发者

Pointer-Array Interaction w/ null terminator

开发者 https://www.devze.com 2023-03-29 03:19 出处:网络
I was just experimenting with the use of pointers when dealing with arrays and I\'ve become a bit confused with how C++ is handling the arrays. Here are the relevant bits of code I wrote:

I was just experimenting with the use of pointers when dealing with arrays and I've become a bit confused with how C++ is handling the arrays. Here are the relevant bits of code I wrote:

//declare a string (as a pointer)
char* szString = "Randy";               

cout << "Display string using a pointer: ";
char* pszString = szString;
while (*pszString)
cout << *pszString++;

First off, when I tried using cout to write what was in "pszString" (without de-referencing)I was a bit surprised to see it gave me the string. I just assumed it was because I gave the pointer a string and no开发者_JS百科t a variable.

What really caught my attention though is that when I removed the asterisk from the line cout << *pszString++; it printed "Randyandyndydyy". I'm not sure why it's writes the array AND then writes it again with 1 letter less. My reasoning is that after writing the char string the increment operator immediately brings the index to the next letter before it can reach the null terminator. I don't see why the null terminator wouldn't cause the loop to return false after the string is output for the first time otherwise. Is this the right reasoning? Could someone explain if I'm getting this relationship between arrays and pointers?


cout has an operator<< overload for char* to print the entire string (that is, print each character until it encounters a 0). By contrast, the char overload for cout's operator<< prints just that one character. That's essentially the difference here. If you need more explanation, read on.

When you dereference the pointer after incrementing it, you're sending cout a char, not and char*, so it prints one character.

So cout << *pszString++; is like doing

cout << *pszString;
pszString = pszString + 1;

When you don't dereference the pointer, you're sending it a char* so cout prints the entire string, and you're moving the start of the string up by one character in each iteration through the loop.

So cout << pszString++; is like doing

cout << pszString;
pszString = pszString + 1;


Illustration with a little loop unrolling:

For cout << *pszString++;

Randy\0
^ pszString points here

// this means increment pszString and send cout the character at which pszString *used* to be pointing
cout << *pszString++;

// so cout prints R and pszString now points
Randy\0
 ^ here

// this means increment pszString and send cout the character at which pszString *used* to be pointing
cout << *pszString++;

// so cout prints a and pszString now points
Randy\0
  ^ here

// and so on

For cout << pszString++;

Randy\0
^ pszString points here

// this means increment pszString and pass the old pointer to cout's operator<<
cout << pszString++;

// so cout prints Randy, and now pszString points
Randy\0
 ^ here

cout << pszString++;

// cout prints andy, and now pszString points
Randy\0
  ^ here

// and so on

I am glad you are experimenting with pointers this way, it'll make you actually know what's going on unlike many programmers who will do anything to get away from having to deal with pointers.

0

精彩评论

暂无评论...
验证码 换一张
取 消