Passing an array pointer to another pointer pointing to the array

Hello,

I'm new to the forum and fairly new to C++ and I have been trying to get a handle on arrays and pointers (ah yes, the confusing land of pointers...). I have been especially perplexed by a particular problem I've encountered in a program that I'm writing, which involves an array of objects that is assigned to a pointer which is then meant to be passed by a function where it can be assigned to a another object's private pointer variable. No matter how many different ways I try to attempt a solution, I either receive a compiler error such as "cannot convert 'a' to 'b' in assignment", or a segmentation fault error at runtime. No doubt this last error is due to that fact that my array did not transfer correctly, causing the computer to fail while attempting to access its location in memory. Here is an simplified example of my latest attempt (I have many different files going on here):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//in file#1

//simple Object class
class Object {
public:
  string name;
  string value;
};

//class Object is encased in a larger class (I've only shown pertinent //functions)
class LargerObject {
private:
  int numObjs;
  Object* objs;

public:
  Object*    getObjs(void) {return objs;}
  Object*    getObj(int index)  {return &objs[index];}
  void       putObjs(Object* obj) {objs = &objs;}
  void       putObj(Object obj, int index) {objs[index] = obj;}
};

//in file#2
int i, j, x, y, z;
Object * myObject[x];
Object * objPtr;
LargerObject** largeObj; 
string someName = "Hello";
string someValue = "world";

largeObj = new LargerObject*[y];

for (i=0; i < x; i++)
{
  largeObj[i] = new LargerObject[z];

  for (j=0; j < x; j++)
  {
    objPtr = new Object;
    objPtr->name = someName;
    objPtr->value = someValue;
    myObject[i] = objPtr;
  }
  objPtr = *myObject;
  largeObj[i][j].putObjs(objPtr); //This seems to be the trouble spot


Can anyone tell me what is the correct way to pass an entire array to a private variable pointer? I've read that it's better to pass a pointer of the array instead of the array itself, which I've tried to do, but seem to be missing something. Help anyone?

Thanks in advance.
I don't understand this part of the code:
1
2
3
4
5
6
7
for (j=0; j < x; j++)
  {
    objPtr = new Object;
    objPtr->name = someName;
    objPtr->value = someValue;
    myObject[i] = objPtr;
  }

You create an Object and store the pointer to this Object in an array. Thats fine, but this loop creates x times Object but store all of them into one element of the array, so x-1 times Object will be lost.

I guess this is a typo:
void putObjs(Object* obj) {objs = &objs;}
It should be {objs = &obj;}, which is not correct either, because this makes objs point to obj. But objs should be a pointer to Object, not a pointer to a pointer to Object. This should be better {objs = obj;}

I hope you have initialized x, y and z in the correct way. It seems to me that x has to be equal y (line 31 and 35)...

largeObj[i][j].putObjs(objPtr); //This seems to be the trouble spot
What is j in this point (j = x, because of the loop before). If x > z this will cause trouble for sure.
mordekai,

Thanks so much! You definitely pointed me in the right direction. I was so busy looking at objects and pointers, I forgot to take a closer look at the for loops. I discovered that I had accidentally called j outside of its loop, hence the segmentation fault was due to something entirely different (geesh, I feel a little sheepish now...).

Thanks again!
Carmen
Topic archived. No new replies allowed.