Pointers in function

M

mangood2005

Hello
How to pass pointer to function and set it to point on particular
address? I'm trying to do this in this simple&sample code, but this
function doesn't change address pointed by pointer named counter. I
know that I can simple change this code to change value of the counter
variable, but what what I want to do for real is to set up pointer
passed as function argument to point on a class object. I hope I wrote
this quite clear.
I would be greatfull for your help

int func(int* counter){
int* pToInt=new int;
*pToInt=5;
counter=pToInt;
return 0;
}
int main(int argc, char* argv[])
{
int* test;
func(test);
coutu<<*test<<endl;
system("Pause");
return 0;

}
 
P

Pete C.

mangood2005 said:
Hello
How to pass pointer to function and set it to point on particular
address? I'm trying to do this in this simple&sample code, but this
function doesn't change address pointed by pointer named counter. I
know that I can simple change this code to change value of the counter
variable, but what what I want to do for real is to set up pointer
passed as function argument to point on a class object. I hope I wrote
this quite clear.
I would be greatfull for your help

int func(int* counter){
int* pToInt=new int;
*pToInt=5;
counter=pToInt;
return 0;
}
int main(int argc, char* argv[])
{
int* test;
func(test);
coutu<<*test<<endl;
system("Pause");
return 0;

}

You need to pass the address of the pointer:

int func(int** counter){
int* pToInt=new int;
*pToInt=5;
*counter=pToInt;
return 0;
}
int main(int argc, char* argv[])
{
int* test;
func(&test);
cout<<*test<<endl;
system("Pause");
return 0;

}

Or you can use references:

int func(int*& counter){
int* pToInt=new int;
*pToInt=5;
counter=pToInt;
return 0;
}
int main(int argc, char* argv[])
{
int* test;
func(test);
cout<<*test<<endl;
system("Pause");
return 0;

}


- Pete
 
P

Pete C.

Pete said:
mangood2005 said:
Hello
How to pass pointer to function and set it to point on particular
address? I'm trying to do this in this simple&sample code, but this
function doesn't change address pointed by pointer named counter. I
know that I can simple change this code to change value of the
counter variable, but what what I want to do for real is to set up
pointer passed as function argument to point on a class object. I
hope I wrote this quite clear.
I would be greatfull for your help

int func(int* counter){
int* pToInt=new int;
*pToInt=5;
counter=pToInt;
return 0;
}
int main(int argc, char* argv[])
{
int* test;
func(test);
coutu<<*test<<endl;
system("Pause");
return 0;

}

You need to pass the address of the pointer:

int func(int** counter){
int* pToInt=new int;
*pToInt=5;
*counter=pToInt;
return 0;
}
int main(int argc, char* argv[])
{
int* test;
func(&test);
cout<<*test<<endl;
system("Pause");
return 0;

}

Or you can use references:

int func(int*& counter){
int* pToInt=new int;
*pToInt=5;
counter=pToInt;
return 0;
}
int main(int argc, char* argv[])
{
int* test;
func(test);
cout<<*test<<endl;
system("Pause");
return 0;

}


- Pete

By the way, you need to remember to delete your pointer. Every new must have
a corresponding delete, every new[] must have a delete[].

- Pete
 
N

Nick Howes

Pete C. said:
mangood2005 wrote:

You need to pass the address of the pointer:

int func(int** counter){

What does it mean to use two asterisks? I have a program working that passes
pointers where the argument is like this,

(int *iscreenX)

and everything else is otherwise the same to how you did it. do you know
what the difference is? I'm just curious really, I'm relatively new to C++
after using Java but i'm getting used to pointers.

thanks

Nick howes
 
K

Karl Heinz Buchegger

Nick said:
What does it mean to use two asterisks? I have a program working that passes
pointers where the argument is like this,

(int *iscreenX)

and everything else is otherwise the same to how you did it. do you know
what the difference is? I'm just curious really, I'm relatively new to C++
after using Java but i'm getting used to pointers.

thanks

Suppose you want to write a function that takes an int,
but you want the function be able to change the int variable
on the callers side.

In C (and in C++ you could) do

void foo( int * arg )
{
*arg = 5;
}

int main()
{
int j;
foo( &j );
}

In other words: instead of passing the int itself, which would result
in sending a copy of j to foo, you pass the address of j to foo. This
way foo is able to change j, it has its address in memory.

Now lets generalize that a little bit: instead of an int, you want to
pass a data type, lets call it T to a function and you want to enable
the function to change the callers variable. As above you write the
function by taking a pointer to T (Attention, this is of course not
compileable. It's intended for demonstration).

void foo( T * arg )
{
*arg = some_T_value;
}

int main()
{
T j;
foo( &j );
}

Now in your example, T is a pointer to int, an int *. Substitute
T with int * and you get:

void foo( int * * arg )
{
*arg = NULL;
}

int main()
{
int * j;
foo( j );
}

That's why there are 2 *. Because T is int *
We say you pass a pointer to pointer to int.

Now C++ has a simpler way to do that: references.

Back to the original example from above. In order to
enable a function to change the callers variable, you
no longer introduce pointers and other funny syntax.
You just tell the compiler: I don't want you to create
a copy, instead the callers variable should be passed to
that function, such that when I change the argument I really
change the callers variable. You do this with a reference:

void foo( int & arg )
{
arg = 5;
}

int main()
{
int j;
foo( j );
}

Again the general model is:

void foo( T & arg )
{
arg = some_T_value;
}

int main()
{
T j;
foo( j );
}

And substituting int* for T leads us to

void foo( int * & arg )
{
arg = NULL;
}

int main()
{
int * j;
foo( j );
}
what the difference is? I'm just curious really, I'm relatively new to C++
after using Java but i'm getting used to pointers.

You need a book on C++. You can't learn a language like C++ without
some sort of printed guidance. Web tutorials and newsgroups are no
replacement for a book (or two, or three).
 
N

Nick Howes

Karl Heinz Buchegger said:
In C (and in C++ you could) do


void foo( T * arg )
T j;
foo( &j );
}

Now in your example, T is a pointer to int, an int *. Substitute
T with int * and you get:

void foo( int * * arg )
{
*arg = NULL;
}

int main()
{
int * j;
foo( j );
}

That's why there are 2 *. Because T is int *
We say you pass a pointer to pointer to int.

Ahar. I think I got confused because Pete C seems to have used a mixture,
with int * * but then calling it by address, i.e. func(&test). Would that be
wrong?
Now C++ has a simpler way to do that: references.

void foo( int & arg )
{
arg = 5;
}

int main()
{
int j;
foo( j );
}

OK, that makes sense. I'll start using references now then as I'm using C++.
I'm getting a book over the summer so that I can get properly to grips with
the nuances.

Thanks for the advice!

Nick h
 
P

Pete C.

Nick said:
Ahar. I think I got confused because Pete C seems to have used a
mixture, with int * * but then calling it by address, i.e.
func(&test). Would that be wrong?

int* p = new int;

To take the address:
int** p_to_p = &p;

That makes a pointer to a pointer. Karl forgot to include the address-of
operator when calling foo.

- Pete
 
N

Nick Howes

Pete C. said:
int* p = new int;

To take the address:
int** p_to_p = &p;

That makes a pointer to a pointer. Karl forgot to include the address-of
operator when calling foo.

Ah okay. I've started using the C++ reference convention for now.
 
K

Karl Heinz Buchegger

Nick said:
Ahar. I think I got confused because Pete C seems to have used a mixture,
with int * * but then calling it by address, i.e. func(&test). Would that be
wrong?

No. It's a mistake made by me.
It must read

int main()
{
int * j;
foo( &j );
}

I didn't compile the examples I posted :)
 
J

JKop

mangood2005 posted:

int func(int* counter){
int* pToInt=new int;
*pToInt=5;
counter=pToInt;
return 0;
}
int main(int argc, char* argv[])
{
int* test;
func(test);
coutu<<*test<<endl;
system("Pause");
return 0;

}


int func(int* &counter)


-JKop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,170
Messages
2,570,925
Members
47,468
Latest member
Fannie44U3

Latest Threads

Top