Today I've learned some concepts about Pointer in C++ and I have a confusion in this code snippet and its result.

Source Code:

// Pointer array
#include <iostream>
using namespace std;

void main () {
	char * say = "Hello";		
	//
	cout << "say = " << say << endl;
	cout << "&say = " << &say << endl;
	cout << "*say = " << *say << endl;
	cout << "say[1]=" << say[1] << endl;
	cout << "*(say+1)=" << *(say+1) << endl;	
	//
	cout << endl << endl;
	for (int i=0; i<5; i++) {
		cout << "say["<< i <<"]=" << say[i] << endl;	
	}
	//
	cout << endl << endl;
	[B]for (int i=0; i<5; i++) {	
		cout << "&say["<< i <<"]=" << &say[i] << endl;
	}[/B]
}

And this is the result:

say = Hello
&say = 0012F3A4
*say = H
say[1]=e
*(say+1)=e

say[0]=H
say[1]=e
say[2]=l
say[3]=l
say[4]=o

[B]&say[0]=Hello
&say[1]=ello
&say[2]=llo
&say[3]=lo
&say[4]=o[/B]

I try to understand why it gave this result, but I cannot understand the section in bold.

Please explain it for me.

Thanks very much,
Nichya.

do you mean &say[i] ? in the loop at the bottom of the program?

When you specify this to output...

&say[0]

it tells cout to start outputing text from the address of say[0] until it hits a null - hence you get 'Hello'.


When you specify this to output...

&say[1]

it tells cout to start outputing text from the address of say[1] until it hits a null - hence you get 'ello'.

etc.

#include <stdio.h>
#include <string.h>

int main()
{
 char szBuffer[]="Hello, World!";

 puts("i       &szBuffer[i]    szBuffer[i]     szBuffer[i]");
 puts("===================================================");
 for(unsigned i=0; i<strlen(szBuffer); i++)
     printf("%u\t%u\t\t%u\t\t%c\n",i,(unsigned)&szBuffer[i],(unsigned)szBuffer[i],szBuffer[i]);

 return 0;
}

/*
i       &szBuffer[i]    szBuffer[i]     szBuffer[i]
===================================================
0       2293600         72              H
1       2293601         101             e
2       2293602         108             l
3       2293603         108             l
4       2293604         111             o
5       2293605         44              ,
6       2293606         32
7       2293607         87              W
8       2293608         111             o
9       2293609         114             r
10      2293610         108             l
11      2293611         100             d
12      2293612         33              !

Process returned 0 (0x0)   execution time : 0.015 s
Press any key to continue.
*/
#include <stdio.h>
#include <string.h>

int main()
{
 char szBuffer[]="Hello, World!";

 puts("i       &szBuffer[i]    szBuffer[i]     szBuffer[i]     szBuffer + i");
 puts("=====================================================================");
 for(unsigned i=0; i<strlen(szBuffer); i++)
 {
     printf
     (
      "%u\t%u\t\t%u\t\t%c\t\t%s\n",
      i,
      (unsigned)&szBuffer[i],
      (unsigned)szBuffer[i],
      szBuffer[i],
      (szBuffer+i)
     );
 }

 return 0;
}

/*
i       &szBuffer[i]    szBuffer[i]     szBuffer[i]     szBuffer + i
=====================================================================
0       2293600         72              H               Hello, World!
1       2293601         101             e               ello, World!
2       2293602         108             l               llo, World!
3       2293603         108             l               lo, World!
4       2293604         111             o               o, World!
5       2293605         44              ,               , World!
6       2293606         32                               World!
7       2293607         87              W               World!
8       2293608         111             o               orld!
9       2293609         114             r               rld!
10      2293610         108             l               ld!
11      2293611         100             d               d!
12      2293612         33              !               !
*/

The last column above, i.e., szBuffer + i, is essentially what you were seeing with your example.

I try to understand why it gave this result, but I cannot understand the section in bold.

cout knows the type and chooses how to present the object. You expect a pointer to char to print a C-style string, and it does. The same thing goes for a pointer to char.

With many satisfactory explanations and detailed source-code like that, it's easy to understand now.

Thanks Frederick and Dave Sinkula very much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.