Please Help In Char Pointers

Well there is this code I recently found in a book and I need help from someone to help me understand why when you subtract pbuffer with buffer you get the number of characters you entered to be read by cin.getline. In other words, what is pbuffer and buffer, what do they contain in reference to how they output the number of characters, you entered in the program?? You will understand what I'm talking about after you read the code below.

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
const int MAX(80); // Maximum array dimension
char buffer[MAX]; // Input buffer
char* pbuffer(buffer); // Pointer to array buffer
cout << endl // Prompt for input
<< "Enter a string of less than "
<< MAX << " characters:"
<< endl;


cin.getline(buffer, MAX, '\n'); // Read a string until \n
while(*pbuffer) // Continue until \0
pbuffer++;
cout << endl
<< "The string \"" << buffer
<< "\" has " << pbuffer - buffer << " characters.";
cout << endl;
return 0;
}

Please Help me understand this code??
Explain what does pbuffer and buffer contain in order to output the number of characters of a string you entered into the program?
Last edited on
Always use code tags with indentation so that the code doesn't looks to be from mars.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
const int MAX(80); // Maximum array dimension
char buffer[MAX]; // Input buffer
char* pbuffer(buffer); // Pointer to array buffer
cout << endl // Prompt for input
<< "Enter a string of less than "
<< MAX << " characters:"
<< endl;


cin.getline(buffer, MAX, '\n'); // Read a string until \n
while(*pbuffer) // Continue until \0
pbuffer++;
cout << endl
<< "The string \"" << buffer
<< "\" has " << pbuffer - buffer << " characters.";
cout << endl;
return 0;
}


ok. now buffer and pbuffer points to the same address in the begining. Lets say 100;

You entered a string - lets say "Hello World". Both the buffers will point to this string and the string will start from address 100;

Now as the string ends, there will be a '0' to mark the end of the string. This is what the while loop is doing by incrementing the address and checking if the value is not '0'.
It will run till it didnt find a '0'. So, your pbuffer will have address equal to 111 at the end of the while loop.

Now your pbuffer has address 111 and buffer has address 100. and thats it - you have 11 characters ?.

Last edited on
So you're saying that the address of pbuffer is 100 since you stated that in your comment. So pbuffer is then incremented in the while loop to have the value of 111. My only question is how does buffer contain the value 100, since buffer is used to output the string as shown in the code. Unless they can be used for two different functions in the same code?
Array names, when written without "[ ]", behave as pointers. Thus, buffer on its own is a pointer. In other words, it's a single number which represents an address in memory.

Read up a bit more about pointers and arrays, and then read up on pointer arithmetic. All will become clear.
Last edited on
Thanks MikeyBoy, I understand that part now.

I know I asked many questions. But there is still one question that troubles me, is that we use the same array name to output the string entered by a user? Why is that the case?

cout<<"The string you entered is: "<<buffer ;

buffer is used to output the string which was not incremented and is still at address 100, i.e. pointing to the start of the string. So, its going to print - "Hello World".
pBuffer points to the end of the buffer, i.e. the ending '0' character and cannot be used to print as its going to print nothing.
Thanks to writeonsharma and to MikeyBoy for the help.
You're welciome :)
Topic archived. No new replies allowed.