Pointer Array problem.

I have following code,

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
void FillArr(int* pt[]);
void PrintArr(int* pt[]);
int main()
{

    int* p[3];
    FillArr(p);
    PrintArr(p);

}

void PrintArr(int* pt[])
{
    for (int x=0;x<3;x++){
        for (int i=0;i<3;i++)
            cout << "Index " << i+1 << " = " << *pt[x]+i << endl;
    }

}

void FillArr(int* pt[])
{
    int Arr1[3]={1,2,3};
    int Arr2[3]={4,5,6};
    int Arr3[3]={7,8,9};

    pt[0]=Arr1;
    pt[1]=Arr2;
    pt[2]=Arr3;
}

But the result of the PrintArr function isn't what i expected. Cane someboby explain me what i do wrong.
You don't need the pointer sign and the subscript operator. (Oh wait I see, you're doing an MD array)
In that case you should read this article which describes why you should avoid md arrays and how to pass them: http://www.cplusplus.com/forum/articles/17108/
Thx for the reply.
Perhaps this seems an MD array, but this is only a example. I must try to solve this as practice. The main passed a array of pointers to the FillArr function. This function point each element of the recived array to a array of int. The main function then calls the PrintArr function to show the content of the int arrays.
When i call the PrintArr function from in the FillArr function the output is correct. But when i call the PrintArr function from the main the output isn't correct.
But when i show the pointers (p[0], p[1], p[2]) of the pointer array self in the FIllArr function and in the main. The are on both place the same.

I'am only a beginner how cane explain me that?

Best Regards, Vic
In main(), p is declared to be an array of 3 pointers to ints. (This is how the 2D array is actually stored in memory)
In FillArray(), you assign those three pointers to be pointers to the arrays declared locally on lines 23, 24, 25.
Those three local arrays are declared on the stack. They go away when FillArr returns, which means p has pointers
to unallocated stack memory.
jsmith is correct.
I do not fully understand what this means, if I may still just trying to understand.
I put after line 29 in FillArr ,
cout << "Address pt[0] = " << pt[0] << endl;
and after line 7 in main following code,
cout << "Address p[0] = " << p[0] << endl;
and in both case the output is 0x22fee0
To my option this maen that i have in main the correct address to the location where Arr1 is stored. But this is a address on the stack and the content of this address is lost by leaving the FillArr function. If this isn't what you maen please correct my.
Maybe a last question, why if i call the PrintArr function after line 29 of the FillArr function the output is corect?
And finally, is there a way by using a pointer array to solve this assignment, or should I look a completely different way to find the sollution, and cane you give me a tip.

Best Regards, Vic
1
2
3
4
5
6
7
8
9
void FillArr(int* pt[])
{
    int v = 0;
    for (int x=0;x<3;x++){
        pt[x] = new int[3];
        for (int i=0;i<3;i++)
            pt[x][i] = ++v;
    }
}

note: dont forget to delete the allocated memory!

To my option this maen that i have in main the correct address to the location where Arr1 is stored. But this is a address on the stack and the content of this address is lost by leaving the FillArr function. If this isn't what you maen please correct my.


That's correct.


Maybe a last question, why if i call the PrintArr function after line 29 of the FillArr function the output is corect?


Because the value hasn't been overwritten yet. Since it is free memory, it can be overwritten; it just so happens in
your case it wasn't. You are relying on undefined behavior.


And finally, is there a way by using a pointer array to solve this assignment, or should I look a completely different way to find the sollution, and cane you give me a tip.


See blackcoder41's response.
Thank you all for the help, I learned a lot from today.

Best Regards,
Vic
Topic archived. No new replies allowed.