pointer to pointer

J

Jack

For the small code below:
#include <iostream>

using namespace std;
int main()
{
char a1[128] = "string 1";
char a2[128] = "string 2";
char a3[128] = "string 3";

char ** p;

p[0] = a1;
p[1] = a2;
p[3] = a3;

cout << "p: "<<*p<<" p1: "<<*(p+1)<<" p2: "<<*(p+2)<< " p3: " << *(p
+3) << endl;

}

The output is:
p: string 1 p1: string 2 p2:

Why p[3] can not be printed out?

Thanks.
 
J

Jim Langston

Jack said:
For the small code below:
#include <iostream>

using namespace std;
int main()
{
char a1[128] = "string 1";
char a2[128] = "string 2";
char a3[128] = "string 3";

char ** p;

p[0] = a1;
p[1] = a2;
p[3] = a3;

cout << "p: "<<*p<<" p1: "<<*(p+1)<<" p2: "<<*(p+2)<< " p3: " << *(p
+3) << endl;

}

The output is:
p: string 1 p1: string 2 p2:

Why p[3] can not be printed out?

1. Because you assigned p[0], p[1] and [3], not [0] [1] and [2].
2. Because you have undefined behavior. You declare p as a pointer to a [n
array of] pointer yet don't allocate any memory for them, and on the
stack just one pointer is allocated. What you probably want is an array of
pointers.

char* p[3];
This allocates on the stack room for 3 pointers, each a pointer to char.
Which would be [0] [1] and [2], incidently, not [0] [1] and [3].

Incidently, once you set up this array of pointers, then you could use a
pointer to pointer to look them also. Which would be (in my sample) pp,
pp+1 and pp+2. Not pp+3.

#include <iostream>

int main()
{
char a1[] = "string 1";
char a2[] = "string 2";
char a3[] = "string 3";

char* p[3];
p[0] = a1;
p[1] = a2;
p[2] = a3;

char** pp;

pp = p;

std::cout << "a1:" << a1 << " p[0]: " << p[0] << " *pp: " << *pp <<
"\n";
std::cout << "a2:" << a2 << " p[1]: " << p[1] << " *(pp+1): " << *(pp+1)
<< "\n";
std::cout << "a3:" << a3 << " p[2]: " << p[2] << " *(pp+2): " << *(pp+2)
<< "\n";
}
 
J

Joe Greer

For the small code below:
#include <iostream>

using namespace std;
int main()
{
char a1[128] = "string 1";
char a2[128] = "string 2";
char a3[128] = "string 3";

char ** p;

p[0] = a1;
p[1] = a2;
p[3] = a3;

You haven't allocated any memory to p, so the above 3 lines are
undefined behavior and in error. p should either be:

char * p[3];

or it should be:

char **p = new char*[3];

or even:

char **p = (char **)malloc(sizeof(char *) * 3);

but you need some memory to store your 3 pointers.
cout << "p: "<<*p<<" p1: "<<*(p+1)<<" p2: "<<*(p+2)<< " p3: " << *(p
+3) << endl;

Down here, you would want to free memory if you allocated some above.
}

The output is:
p: string 1 p1: string 2 p2:

Why p[3] can not be printed out?

Fix the memory problems and it should work better.

joe
 
G

Guest

For the small code below:
#include <iostream>

using namespace std;
int main()
{
char a1[128] = "string 1";
char a2[128] = "string 2";
char a3[128] = "string 3";

char ** p;

p[0] = a1;
p[1] = a2;
p[3] = a3;

cout << "p: "<<*p<<" p1: "<<*(p+1)<<" p2: "<<*(p+2)<< " p3: " << *(p
+3) << endl;

}

The output is:
p: string 1 p1: string 2 p2:

Why p[3] can not be printed out?

Wrong question, the real question is how big the probability that the
first two will print is. What you have is called undefined behaviour, p
is a pointer to a pointer to a char, not an array, which means that no
memory is allocated for it, so your assignments are writing to some
unknown place in memory and you are just lucky nothing bad happens.

To correct your code change the declaration of p to either

char* p[4];

or

char** p = new char*[4];

If you go with the latter, do not forget to free the memory when you are
done with it.

Whit that done, let us look a bit at the following line:

cout << "p: "<<*p<<" p1: "<<*(p+1)<<" p2: "<<*(p+2)<< " p3: " << *(p
+3) << endl;

The problem is when you try to print *(p+2), which is the same as p[2].
If you look at the code where you assigned the the strings to p, you
never assigned anything to p[2] (you assigned to p[1] and p[3] but not
p[2]), which means that whatever is at p[2] is garbage. So when the
program tries to dereference the pointer at p[2] it will dereference
garbage and anything can happen.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top