Skip to main content
gamedev.net
🔒 Locked

passing pointers into functions

Started by Psycho_68 8 replies 400+ views
Original Post
Psycho_68
Psycho_68
hi, ive not been using c++ for a quite long time now and i feel quite stupid about this, ... ,but here goes: im trying to pass a pointer to a function so when i change it in the function it changes also the value in the rest of the program, when i try this: #include <iostream.h> void func(int* pass) { int bla = 15; int* ho = &bla cout << *pass << "\n"; pass = ho; cout << *pass << "\n"; } int main(int argc, char* argv[]) { int aaa = 888; int* ptr = &aaa cout << *ptr << "\n"; func(ptr); cout << *ptr << "\n"; return 0; } this dont work, a new pointer is created with a new adress within the function and it doesnt change the value outside the function, i need someway to pass the pointer to the function, so a new one is not created, or is created with the same adress as the one outsuide the function, so i can change the value outside the function within the function ive also come up with this code, which works, but uses references, i would like to get it to work without them void func(int& pass) { int bla = 15; int* ho = &bla cout << pass << "\n"; pass = *ho; cout << pass << "\n"; } int main(int argc, char* argv[]) { int aaa = 888; int* ptr = &aaa cout << *ptr << "\n"; func(*ptr); cout << *ptr << "\n"; return 0; }
Koen
Koen
Try this:

void func(int* pass)
{
int bla = 15;
cout << *pass << "\n";
*pass = bla;
cout << *pass << "\n";
}

You were assigning the adress-value, while you wanted to assign the actual integer value. Therefore, you need to dereference the pointer (*pass, which indicates the value pass points to).
Yanroy
Yanroy
If you''re trying to change the value that is being pointed to, then you need to difference the pointer before assigning to it:

void func(int* pass)
{
int bla = 15;
//int* ho = &bla
cout << *pass << "\n";
*pass = bla;
cout << *pass << "\n";
}

But that''s probably not what you''re trying to do... my guess is you want to change the address being pointed to. In which case, you need a pointer to a pointer

void func(int** pass)
{
int bla = 15;
int* ho = &bla
cout << *pass << "\n";
*pass = ho;
cout << *pass << "\n";
}

Now when the function returns to main(), ptr will be equal to the address of bla. You''ll have to call func() like this in main(): func(&ptr);

NOTE: this will probably crash if you try to use the pointer, because bla is created locally in func() and thus goes out of scope when func() returns.

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
Psycho_68
Psycho_68
thx for the help all
this think doesnt work:

#include <iostream.h>
void func(int* pass)
{
int bla = 15;
int* ho = &bla
cout << *pass << "\n";
pass = ho;
cout << *pass << "\n";
}

int main(int argc, char* argv[])
{
int aaa = 888;
int* ptr = &aaa
cout << *ptr << "\n";
func(&ptr);
cout << *ptr << "\n";

return 0;
}

it gives this error:

C:\Windows\Plocha\lam\lam.cpp(19) : error C2664: ''func'' : cannot convert parameter 1 from ''int ** '' to ''int *''
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

lam.exe - 1 error(s), 0 warning(s)

I really wanna change the adress so this dont help>
void func(int* pass){int bla = 15;cout << *pass << "\n";*pass = bla;cout << *pass << "\n";}

thx for the help anyway.

so i tryed this from Yanroy>

void func(int** pass)
{
int bla = 15;
int* ho = &bla
cout << *pass << "\n";
*pass = ho;
cout << *pass << "\n";
}

it works, somehow, the first cout prits the number, but the other 2 print a adress and the last one prints a weird number

think i need to work on this a little more, btw i never heard of pointers to pointers, i think i may stick with references, heh
Psycho_68
Psycho_68
uhh anybody got some more ideas, other than those references, but those pointers to pointers look even more evil heh
Trienco
Trienco
i get the feeling you might need to get a better understanding of pointers and how they work.

what is this ho all about? it is completely pointless, never used and passing something over 20 useless variables wont change a thing. so throw it out.

void func(int** pass)
{
int bla = 15;
cout << *pass << "\n";
*pass = &bla
cout << *pass << "\n";
}

now you output *pass but seem to expect to get a number. how? pass is a pointer to a pointer to a int and dereferencing it just once will still be a pointer, not a number.

cout << **pass << "\n";

then you would still have a pointer to an address thats invalid as soon as you leave the function. so:
a) bla wont exist anymore
b) your pointer to the address of bla wont make any sense anymore
c) trying to dereference *pass will most likely crash

if this is just for testing inside func its ok, but you should _never_ set a pointer from "outside" to a temporary inside the function without expecting access violations if you ever try to use the pointer outside the function.
f@dzhttp://festini.device-zero.de
Yanroy
Yanroy
Heh... I screwed up... in the code I posted, the cout << *pass should have been cout << **pass

--------------------


You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming

You are unique. Just like everybody else.

"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor
Psycho_68
Psycho_68
quote:
Original post by Trienco
i get the feeling you might need to get a better understanding of pointers and how they work.


we were dooing Java at school and, I kinda forgotten what c++ is like, i think i get the picture now, only the pointer to poiter thing surprized me
quote:
Original post by Trienco
if this is just for testing inside func its ok, but you should _never_ set a pointer from "outside" to a temporary inside the function without expecting access violations if you ever try to use the pointer outside the function.

I get the picture so this is wrong, cause the pointer gets asigned an adress which goes out of scope as soon as the function quits: ??

void func(int** pass)
{
int bla = 15;
cout << *pass << "\n";
*pass = &bla
cout << *pass << "\n";
}

I think Ill use references then, im kinda getting to like them lately.

[edited by - Psycho_68 on July 17, 2003 3:27:51 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.