Printing Array of Pointers

Doing a school project, etc. etc.

1
2
3
4
for (j = 0; j <= PlaneAlfa.NumPartiesOnPlane; j++)
{
cout << PlaneAlfa.Parties[j];
}


PlaneAlfa is a Structure, Parties is an array of pointers, containing names.

How exactly would you print the entire array? Putting an asterisk anywhere doesn't seem to help, and I can't find anything on the internet.

When I try

cout << PlaneAlfa.Parties[j] << endl;

I get an error upon compiling:

no operator "<<" matches these operands"


I'm not sure if I'm just not dereferencing correctly or what.
Last edited on
I would guess that this might do:

1
2
3
4
for (int j = 0; j != PlaneAlfa.NumPartiesOnPlane; j++)
{
cout << PlaneAlfa.Parties[j]<<endl;
}


Key difference here being the != (not equal to) instead of your <= (less or equal to) which will fail when it tries to read the last element behind the end of the array.
Last edited on
A bad oversight on my part with the for loop!

Probably should have explained what the problem is better. When I try

cout << PlaneAlfa.Parties[j] << endl;

I get an error upon compiling:

no operator "<<" matches these operands"


I'm not sure if I'm just not dereferencing correctly or what.
Last edited on
1
2
3
4
for (int j = 0; j < PlaneAlfa.NumPartiesOnPlane; j++)
{
     cout << (*PlaneAlfa.Parties[j]).name << endl;
}


A pointer is just a hex value like 0xFF0023334. To get to thing it is pointing to you'll have to dereference the pointer with one of two ways:

1) (*pointer).data;

or

2) pointer->data;

If the pointer is pointing to a primitive (not pointing to a struct or class), you can just do this:

(*pointer)

so you'd have:

1
2
3
4
for (int j = 0; j < PlaneAlfa.NumPartiesOnPlane; j++)
{
     cout << (*PlaneAlfa.Parties[j]) << endl;
}
Last edited on
RPGillespie,
if PlaneAlfa is a structure, and PlaneAlfa.Parties is a pointer, then PlaneAlfa.Parties[j] will return the jth element of Parties providing the pointer is actually to an array. Or to simplify, if Parties is an array Parties[j] will index the array and return its value.

pointer->data will return element data from a pointer to a class/structure called pointer. Assuming that what Kainunno said about his PlaneAlfa structure being a structure, and Parties being an array, then your code will try to dereference a value, which will fail.

Kainunno,
I'm not familiar with that error, but it sounds like the compiler isn't realising that cout is a stream, have you added the #define <iostream> directive?
ausairman,

Kainunno said "Parties is an array of pointers, containing names." Which means that he did something like this:

 
??? * Parties[x];


Where ??? is the type. He never specified what type of pointer Parties contains. If it is a pointer to a struct, he will use pointer->data, otherwise he will use (*pointer).
Last edited on
I have #include <iostream> and every other cout works.

I may just be completely misunderstanding pointers, so here's some of the relevant code.

My structures, Plane and Party:

1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef PLANE_H
#define PLANE_H
#include "Party.h"

struct Plane
	{
	long	MaxSeats;
	long	NumSeatsOccupied;
	long	NumPartiesOnPlane;
	Party *	Parties;
	};

#endif 


1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef PARTY_H
#define PARTY_H

enum Planes {ALFA, BRAVO, INVALID_PLANE};

struct Party
	{
	char *	Name;
	long	Size;
	Planes	WhichPlane;
	};

#endif 


And then in my main .cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
PlaneAlfa.Parties = new Party [PlaneAlfa.NumPartiesOnPlane];
//Cutting an irrelevant chunk out
if ((TempParty.Size + PlaneAlfa.NumSeatsOccupied) <= PlaneAlfa.MaxSeats)	//place party in Alfa
{
	PlaneAlfa.Parties [PlaneAlfa.NumPartiesOnPlane++] = TempParty;
	PlaneAlfa.NumSeatsOccupied =+ TempParty.Size;
	cout << TempParty.Name << " has boarded plane Alfa." << endl;
}
//Cutting an irrelevant chunk out
for (j = 0; j < PlaneAlfa.NumPartiesOnPlane; j++)
{
        cout << (*PlaneAlfa.Parties[j]).name << endl;
}


It's supposed to print a list of parties, and then delete them so that other parties many "fly."

And NOW I realize that I had forgotten that Name was ALSO a pointer and also had to be dereferenced.

So.. That was my problem!
Last edited on
Ah right, sorry RPGillespie I misread...
Topic archived. No new replies allowed.