Using an array of pointers

Ok So I think I've completely confused myself now.

I've created an Array of pointers to objects using:

Person ** A = new person * [arraysize];

When I intend to access a specific person do I have to do this? :

something = A->[i];

and when I want a specific object within my struct do I have to do this? :

something_else = A->[i]->random_int;

Pointers kind of confuse me.
You access the array members just like you would any other, so that

A[i]

is a pointer to a person.

You can use that pointer like you would any other, so:

something_else = A[i]->random_int

If you can break it down logically in your mind, it makes sense. You can make it a bit clearer for yourself by making that break-down explicit, e.g.

1
2
Person* aPerson = A[i];
something_else = aPerson->random_int

Last edited on
Oh, so A[i] will automatically follow the pointer to the specific person and A[i]->random_int will automatically access the int in the person?

Did you define it that way the second time to show me that it works the same way, or did you not see that I defined it person ** A = new person * [arraysize]. Not being arrogant, just asking.
to show you it works the same way i'd imagine.
splitting it up is useful for debugging as well. sticking a breakpoint on line 2 allows you to look at the person object.
Alright I appreciate it guys. Good, concise advice. Thank you.
Oh, so A[i] will automatically follow the pointer to the specific person and A[i]->random_int will automatically access the int in the person?

Well, A[i] is just an array element. It will be whatever type you've defined your array as. If it's an array of ints, then it's an int. If it's an array of pointers to Person, then it's a Person*. The symbol A[i] will behave as a variable of that type, whatever the type is.

Did you define it that way the second time to show me that it works the same way, or did you not see that I defined it person ** A = new person * [arraysize]. Not being arrogant, just asking.


Um, I was showing you how to logically break down the process of accessing members of your structs. If you want me to make it more complete (and correct your typo):

1
2
3
4
5
6
7
Person ** A = new Person* [arraysize]; // Declare an array of Person*

// Presumably, some code to populate the array with pointers to actual Person objects

Person* aPerson = A[i]; // Store the array element in its own variable

something_else = aPerson->random_int; // Get the value of a member of the Person object 
closed account (o3hC5Di1)
Hi there,

What helped me was to think of pointers as any other variable, but instead of numbers or text they contain addresses of memory. You could print a pointer like this:

1
2
3
4
int a;
int* p = &a;

std::cout << p;


It will print out the current address of the first byte of 'a'. In other words, the pointer refers to the space in memory where 'a' is. Now, that's not usually what we need, we usually want to access the value in that memoryspace. Accessing that value means we have to dereference that address.

The dereference operator for scalar types is *:

1
2
3
int a = 3;
int* p = &a;
std::cout << *p; //prints 3 


For classes and structs however, we use the arrow operator:

1
2
3
4
5
6
7
struct my_struct
{
    int member;
} instance;

my_struct* p = &instance;
std::cout << p->member;



When you have an array, the brackets actually serve as a dereferencing operator:

1
2
3
int arr[10];

//arr[5] == arr + 5 


The name of the array is actually a pointer to the start of the array, the brackets count 5 spaces further and dereference it for you. This happens under the hood, so you don't really realize it happens.


Now we can discuss pointers to pointers:

1
2
3
4
5
6
7
8
9
10
11
int a=3;
int* p = &a;
int** ptp = &p;

std::cout << p; //shows address of a
std::cout << ptp; // shows address of p

std::cout << *p; //shows 3
std::cout << *ptp; //shows address of a (content of p)

std::cout << *(*ptp); //shows 3 



Now to your example, you said you wanted an array of pointers to objects:

1
2
3
4
5
6
7
//remember, arr is a pointer in its own way
Person** arr = new person* [size];
Person* p = new Person();
arr[0] = p;

std::cout << arr[0]; //prints address of first person
std::cout << arr[0]->name; //prints the name member of the first person 


The []-operator has precedence over the -> operator (because it is to the left of it), so what happens is this:

arr[0]->name => p->name


Hope that helps.

All the best,
NwN
Wait I thought what you guys said was working, but for some reason it's not. I don't know if I just coded wrong. Should this work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>

struct person {
        string fname, lname;
        int dob, ssn;
        void print();
};

void person :: print() {
        cout << ssn << " " << fname << " " << lname << " " << dob << "\n";
}

void main() {
        person ** A = new person * [3];
        A[0] -> ssn = 1;
        A[0] -> dob = 1;
        A[0] -> fname = "Joe";
        A[0] -> lname = "Fraiser";
        A[0] -> print();
}


Please ignore the void main. That's just how the school complier works.
All you've done in that code is created an array of pointers. Those pointers don't actually point to anything at the moment. You haven't actually created any person structures for them to point to.

Edit: You can do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
const int NUM_PERSONS = 3;

person ** A = new person * [NUM_PERSONS];
for (int personIdx = 0; personIdx  < NUM_PERSONS; ++personIdx)
{
  A[personIdx] = new person;
  A[personIdx] -> ssn = 1;
  A[personIdx] -> dob = 1;
  A[personIdx] -> fname = "Joe";
  A[personIdx] -> lname = "Fraiser";
  A[personIdx] -> print();
}
Last edited on
If I'm taking in text from a file, is there an easy way to initialize them first so that I can loop the assignments I did in main? Where would I need to do that?
closed account (o3hC5Di1)
Hi there,

You have created an array of pointers, but you have not created any objects to point to:

16
17
18
19
20
21
22
23
24
25
26
void main() {
        person ** A = new person * [3]; //creates the array
        person* p1 = new Person(); //creates a new person on the heap

        A[0] = p1; //put the pointer into the array
        A[0] -> ssn = 1;
        A[0] -> dob = 1;
        A[0] -> fname = "Joe";
        A[0] -> lname = "Fraiser";
        A[0] -> print();
}


Hope that helps.

All the best,
NwN
Topic archived. No new replies allowed.