Archived

This topic is now archived and is closed to further replies.

how to set a pointer as a parameter?

This topic is 5150 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok here''s the deal ive got two global pointers: float *myverts; float *mytexcoords; it doesnt point to anything yet... i have a function, that builds two arrays, but cant return them because there are two, so i need the programmer to pass two paramters, each should be pointers that will point to the built array''s MakeTheArrays(somestuff,myverts,mytexcoords); and the function should make myverts, and mytexcoords, point to locations of the newly built arrays...
void MakeTheArrays(int somestuff,float *wverts,float *wtexcoords)
{
    float *verts = new float[somestuff];
    float *texcoords = new float[somestuff];

    ...
    
    wverts=verts;
    wtexcoords=texcoords;
    ...
}
unfortunately this doesnt work could someone help me?

Share this post


Link to post
Share on other sites
Elementary my dear Fireking.
Pass your argument by reference.


int *g_foo;

void CreateFoo( int* &foo )
{
foo = new int[100];
}



[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
You can declare your function:
void MakeTheArrays(int somestuff,float * & wverts,float * & wtexcoords)
it should work the way you have the function coded.

Share this post


Link to post
Share on other sites
thanks guys, but im not sure if my new array line is working...

float *something= new float[somethingcount];

is that the correct syntax for making the array?

like

something[0]
something[1]
...
something[25245]

thanks for the help so far, i dont know what i would do without this forum

Share this post


Link to post
Share on other sites
It looks correct. What''s the problem ?

Using a std::vector<float> would probably be a good idea.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
well i was just wondering, because the debugger shows the contents of variables (vs.net), and it shows verts, the address, and if you expand the node, it shows 0.000000 and thats it...

Share this post


Link to post
Share on other sites
So what ? You''re in debug mode and the compiler zeroed out the memory for you ? No big deal


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Please can someone explain why fireking''s original code doesn''t work? Is it not valid to pass an array to a function and modify it?? Why the hell doesn''t it work?! I''m confused...

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Please can someone explain why fireking''s original code doesn''t work? Is it not valid to pass an array to a function and modify it?? Why the hell doesn''t it work?! I''m confused...


Because you''re PASSING the address of the array. You can therefore modify the data at this address without issue.

If you do:
void Test(int x)
{
x= 10;
}

This will NOT modify the original value passed in, similar to passing an array''s memory address. You CANNOT modify it''s memory address, just the values that it points to. When you pass it by reference, or a double pointer (I''ll show this below), you are then passing the address of the variable that stores the address in memory, so you can now modify the address in memory that it points to.


Rather than references, you can also do this:
void MakeTheArrays(int somestuff, float **wverts, float **wtexcoords)
{
float *verts = new float[somestuff];
float texcoords = new float[somestuff];

*wverts = verts;
*wtexcoords = texcoords;
}

It''s a bit more complicated looking, but achieves the same results . (This is what we had to do before references came about).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Thanks, but I still don''t fully understand...

I know about passing by value and passing by address, but I don''t see the difference between passing a pointer or passing a pointer to the first element in an array:

void Foo(float *data)
{
data = new float;
}

void Foo(float *data)
{
data = new float[20];
}

What is wrong with this?? Please can someone help me out...

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Imagine this

foo(somefloat);

void foo(float x)
{
x = 10;
}

The somefloat value wouldn''t change right?

Now imagine that x is *data

void foo(float *data)
{
data = 10;
}

It still wouldn''t change!
The pointer is just saying that it''s not a real value, but a memory adress. Therefore you can change the data IN THAT MEMORY ADRESS, but you can''t change the Pointer itself, for that you need to pass by reference (with the &. The * is just like another datatype if you don''t pass it by reference you can''t change it.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I am the same AP who brought up this issue...

quote:
Original post by Anonymous Poster
Imagine this

foo(somefloat);

void foo(float x)
{
x = 10;
}

The somefloat value wouldn''t change right?

Now imagine that x is *data

void foo(float *data)
{
data = 10;
}

It still wouldn''t change!
The pointer is just saying that it''s not a real value, but a memory adress. Therefore you can change the data IN THAT MEMORY ADRESS, but you can''t change the Pointer itself, for that you need to pass by reference (with the &. The * is just like another datatype if you don''t pass it by reference you can''t change it.


What the heck are you talking about, of course you can pass in a pointer and modify it. This works perfectly:

void Foo(float *data)
{
data = new float;
}

The above is fine, but in your post you are saying you can''t change what the pointer points to? Rubbish.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I am the same AP as above.

So, in short, you are telling me that you can''t change what a pointer points to when you pass it into a function like this:

void foo(float *ptr) //-> Not possible to change what the pointer points to??

Share this post


Link to post
Share on other sites
consider these two functions;

foo * bar;
void ChangeStuff(foo * bar)
{
// do stuff with the data bar points to

}

void ChangeAddress(foo *&bar)
{
// change the pointer data

}

void MyFunc(void)
ChangeStuff(bar);
ChangeAddress(bar);


Now, the first function is passed the address of some memory pointed to by ''bar'', as you would expect.
The second function is passed the address of the varible ''bar'' which is of type ''foo *''

Now, if you did it as following (using the same fucntions above)

foo bar[20];
ChangeStuff(bar);
ChangeAddress(bar);


The Changestuff function is given a pointer to the first element of the array, where as the ChangeAddress function is passed the address of the varible which holds the address of the array, and so by changing that varible you can in effect lose that section of memory.
Another way to put it would be to say that assume the varible ''bar'' exists at address 0x1 and points to the memory at 0x20. The first function gets a pointer to 0x20 and the second gets a pointer to 0x1.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Anonymous Poster
I am the same AP who brought up this issue...

...
What the heck are you talking about, of course you can pass in a pointer and modify it. This works perfectly:

void Foo(float *data)
{
data = new float;
}

The above is fine, but in your post you are saying you can''t change what the pointer points to? Rubbish.


No, that doesn''t work. Go ahead and try it and don''t come back until you do. You''ll either get an error because you are using an undefined pointer or if you do initialize (with new) before calling it, the value of ''data'' will remain the same outside that function.

What I meant is that you CAN change the stuff that is IN THAT MEMORY ADDRESS (data) using something like *data = 10.0f;
BUT if you do data = something; The value of data OUTSIDE the function WILL NOT CHANGE because you are not passing by reference.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
Thanks, but I still don't fully understand...

I know about passing by value and passing by address, but I don't see the difference between passing a pointer or passing a pointer to the first element in an array:

void Foo(float *data)
{
data = new float;
}

void Foo(float *data)
{
data = new float[20];
}

What is wrong with this?? Please can someone help me out...


Hi.

Its quite simple really. Consider the following (rather useless)code.


void resetpointer(int* pfunctionint)
{
pfunctionint = 0;
}

int main()
{
int* pint = new int(3);
resetpointer(pint);
delete pint;
return 0;
}


Here we have just created a simple pointer to an int, and stored an address from the free store in it (assuming operator new doesnt throw an std::bad_alloc).

Now consider the call to the function resetpointer(). We know from the function definition that the pointer will be passed by value, meaning that the copy constructor of int* will be called to construct pfunctionint. This will result in both pointers pointing to the same location ( meaning both pointers contain the same address ).


pint points to a free store address
pfunctionint points to the same free store address


So we have two seperate variables which both have the same value stored in them. Now consider the next line:


pfunctionint = 0;


All this simply does is set the value in pfunctionint to 0, obviously. But the assignment doesnt effect pint in anyway, because they are two completly different variables which happen to have the same value stored in them.


pint points to a free store address
pfunctionint points to 0


When the function ends, pfunctionint is destroyed, leaving pint pointing to the exact same memory address as it did before.





[edited by - Jingo on November 9, 2003 11:54:53 AM]

[edited by - Jingo on November 10, 2003 4:09:59 AM]

Share this post


Link to post
Share on other sites
here''s what''s happening



void func1(int x, int * y)
{
x = 10;
(*y) = 11;
y = 0;
}

void func2(int &x, int * &y)
{
x = 12;
(*y) = 13;
y = 0;
}

void main()
{
int a = 1;
int b = 2;
int * c = &b;

printf("original a = %d, b = %d, c = %d\n", a, b, c);
func1(a,c);
printf("after func1() a = %d, b = %d, c = %d\n", a, b, c);
func2(a,c);
printf("after func2() a = %d, b = %d, c = %d\n", a, b, c);
}



the output from my compiler looks like this:

original a = 1, b = 2, c = 1244872
after func1() a = 1, b = 11, c = 1244872
after func2() a = 12, b = 13, c = 0



in func1() we pass a by value, and c, a pointer to b, by value.
- when we change x (a by value) we don''t change a.
- when we change the value pointed to by y (*y), we really
change b.
- when we change the value of y, (c by value), we don''t change
c.

in func2() we pass a by reference, and c, a pointer to b, by reference
- when we change x (a by ref), we really change a.
- when we change the value pointed to by y, we really change b.
- when we change the value of y(c by ref) we really change c.


if you had :

void func3(int * x)
{
x = new int[5];
}

void func3(int * &x)
{
x = new int[5];
}

void main()
{
int * a = 0;
func3(a);
printf( "a = %d\n", a);
func4(a);
printf( "a = %d\n", a);
}


we''d get:

a = 0
a = 1244872

or something like that... when we pass a by value into func3,
we can''t change the address a points to, only the value held at
that address.
When we pass a by reference into func4, we can actually change
the value of a.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Wow, thanks guys, and I thought that I understood pointers well...

So just to clarify, pointers have 2 addresses, the actual address of the pointer, and the actual address of the object it is pointing to. Is this correct?

So if you have:

int *pInt = &someOtherInt;

So is the address of ''pInt'' the same address as ''&someOtherInit''??

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster
int *pInt = &someOtherInt;

So is the address of ''pInt'' the same address as ''&someOtherInit''??


The *value* of pInt will be the *address* of someOtherInt.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
i dunno if this will help the situation any, or if anyone has already said this since i didn''t read every word of every post, but...

pointers are just memory addresses. in fact, they are unsigned integers that happen to represent a place in memory. if u have something like float * floatptr = &myfloat, then floatptr holds the memory address of myfloat, which in this case is memory on the stack. why do pointers have types (i.e. why are there float *''s and int *''s and char *''s)? so the compiler can know what value to add to a pointer when u are indexing it.

suppose u have char * arrayptr that has the value 0x00aabbcc (11189196 in base 10). since the pointer is a char *, and a char data type is 1 byte, if u specify ''arrayptr[1]'' the compiler will fetch the 1-byte value at 0x00aabbcc + 1, or 0x00aabbcd.

or, if u have a pointer to a structure that holds, say 3 floats (so, twelve bytes since each float is 4 bytes), then multiples of twelve will get added to whatever number u use to index the pointer. structptr[3] is actually adding 3 * 12 to whatever value structptr is.

u can use any integral value as a pointer if u type cast it as one. conversely, u can use any pointer as an integral value. they are one and the same.

getting back to the original question, i believe ready4dis had the simplest idea - just have the function take the address of the pointer (i.e. a pointer to a pointer), and set the pointer either by dereferencing or indexing (i.e. *ptr or ptr[0]).

Share this post


Link to post
Share on other sites