Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,209 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 910 people online right now. Registration is fast and FREE... Join Now!




Passing array address through a class function

 
Reply to this topicStart new topic

Passing array address through a class function

lichmaster03
post 13 Mar, 2008 - 10:03 AM
Post #1


New D.I.C Head

*
Joined: 13 Mar, 2008
Posts: 2

I am getting this warning when compiling.

QUOTE
warning C4700: uninitialized local variable 'p' used


These are the functions that use 'p'.

CODE
void Grid::init(Array mary){

    Cell ***p;
    
    mary.tempPointer(p, mary);
    
    d = (mary.getD())/2;
    r = (mary.getR())/2;
    c = (mary.getC())/2;

    p[d][r][c].current();
}
void Array::tempPointer(Cell ***p, Array mary){
    p = mary.ary;
}


Any help will be greatly appreciated.
User is offlineProfile CardPM

Go to the top of the page

Bench
post 13 Mar, 2008 - 10:40 AM
Post #2


D.I.C Addict

Group Icon
Joined: 20 Aug, 2007
Posts: 601



Thanked 10 times

Dream Kudos: 150

Expert In: C/C++

My Contributions


You're passing the pointer by value. This means your Array::tempPointer function isn't modifying the original pointer object, but making a copy instead, so the original remains uninitialised after the call to tempPointer.

If you wish to modify the original, then possibly the simplest way is to pass by reference;
cpp
void Array::tempPointer(Cell& ***p, Array mary){
p = mary.ary;
}

I'd probably also question why you're using a pointer-to-pointer-to-pointer, but that's a separate issue altogether

This post has been edited by Bench: 13 Mar, 2008 - 10:42 AM
User is offlineProfile CardPM

Go to the top of the page

lichmaster03
post 13 Mar, 2008 - 01:03 PM
Post #3


New D.I.C Head

*
Joined: 13 Mar, 2008
Posts: 2

Thanks for the help but now I'm having trouble getting the address of p to match the address of mary.ary.

CODE
Grid::Grid(Array &mary){
    this->init(mary);
}
void Grid::init(Array &mary){
    
    p = mary.tempPointer(mary);
    
    d = (mary.getD())/2;
    r = (mary.getR())/2;
    c = (mary.getC())/2;

    p[d][r][c].current();

    cout << '\n' << p;
}
Cell*** Array::tempPointer(Array& mary){
    return mary.ary;
}


I passed the array by reference so p could = mary.ary but the addresses dont match when I print them.

This post has been edited by lichmaster03: 13 Mar, 2008 - 01:03 PM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 07:36PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month