Newbie question on pointer

J

Jason

Hello. I am trying to learn how pointer works because I want to write
some functions that can return multiple values. Here is my code:

//start
#include <iostream>
#include <stdlib.h>

using namespace std;
bool search(int value, int size, const int array[], const
int*location);
const int *search2(int, int, const int*);

int main(int argc, char *argv[])
{
bool success = false;
const int array[10] = {1,2,3,4,5,6,7,8,9,10};
const int *found_address = NULL;
found_address = search2(2, 10, array);
if(found_address != NULL)
cout <<" Found item at array address: "<< found_address<<endl;

found_address = NULL;
success = search(2, 10, array, found_address);
if(success) cout <<" Found item at array address: "
<< found_address<<endl;

system("PAUSE"); //prevents my window xp from
//terminating my console disp.
return 0;
}
//why doesn't this work
bool search(int value, int size, const int array[], const int*l)
{
int i = 0;
for(i = 0; i<size;i++)
{
if(array == value)
{
l = &array;
return true;
}
}
return false;
}
//I know this work
const int * search2(int value, int size, const int array[])
{
int i = 0;
for(i = 0; i<size;i++)
{
if(array == value)
{
return &array;
}
}
return NULL;
}
//end

my question is: why doesn't my SEARCH function work? I pass a pointer
to SEARCH function and assign it an address. Then when I finish the
call, the memory address should be save in the pointer, but it
doesn't. How do I make it work?
 
G

Gianni Mariani

Jason said:
Hello. I am trying to learn how pointer works because I want to write
some functions that can return multiple values. Here is my code:

//start
#include <iostream>
#include <stdlib.h>

using namespace std;
bool search(int value, int size, const int array[], const
int*location);
const int *search2(int, int, const int*);

int main(int argc, char *argv[])
{
bool success = false;
const int array[10] = {1,2,3,4,5,6,7,8,9,10};
const int *found_address = NULL;
found_address = search2(2, 10, array);
if(found_address != NULL)
cout <<" Found item at array address: "<< found_address<<endl;

found_address = NULL;
success = search(2, 10, array, found_address);
if(success) cout <<" Found item at array address: "
<< found_address<<endl;

system("PAUSE"); //prevents my window xp from
//terminating my console disp.
return 0;
}
//why doesn't this work
bool search(int value, int size, const int array[], const int*l)

Maybe you want to pass l by reference ...
bool search(int value, int size, const int array[], const int* &l)

See:

http://www.parashift.com/c++-faq-lite/references.html
 
J

John Harrison

Jason said:
Hello. I am trying to learn how pointer works because I want to write
some functions that can return multiple values. Here is my code:

//start
#include <iostream>
#include <stdlib.h>

using namespace std;
bool search(int value, int size, const int array[], const
int*location);
const int *search2(int, int, const int*);

int main(int argc, char *argv[])
{
bool success = false;
const int array[10] = {1,2,3,4,5,6,7,8,9,10};
const int *found_address = NULL;
found_address = search2(2, 10, array);
if(found_address != NULL)
cout <<" Found item at array address: "<< found_address<<endl;

found_address = NULL;
success = search(2, 10, array, found_address);
if(success) cout <<" Found item at array address: "
<< found_address<<endl;

system("PAUSE"); //prevents my window xp from
//terminating my console disp.
return 0;
}
//why doesn't this work
bool search(int value, int size, const int array[], const int*l)
{
int i = 0;
for(i = 0; i<size;i++)
{
if(array == value)
{
l = &array;
return true;
}
}
return false;
}
//I know this work
const int * search2(int value, int size, const int array[])
{
int i = 0;
for(i = 0; i<size;i++)
{
if(array == value)
{
return &array;
}
}
return NULL;
}
//end

my question is: why doesn't my SEARCH function work? I pass a pointer
to SEARCH function and assign it an address. Then when I finish the
call, the memory address should be save in the pointer, but it
doesn't. How do I make it work?


You assigned l in search a value, but l only exists in search, it has
nothing to do with found_address in main.

You need a pointer to a pointer.

bool search(int value, int size, const int array[], const int **location);

const int *found_address = NULL;
found_address = NULL;
success = search(2, 10, array, &found_address);

bool search(int value, int size, const int array[], const int**l)
{
....
* l = &array;

Now search has the address of you pointer in main (instead of just a copy)
so it can modify the pointer in main.

john
 
Y

Yakov Lerner

Hello. I am trying to learn how pointer works because I want to write
some functions that can return multiple values. Here is my code:
...
bool search(int value, int size, const int array[], const
int*location);

int main(int argc, char *argv[])
{
bool success = false;
const int array[10] = {1,2,3,4,5,6,7,8,9,10};
const int *found_address = NULL;
...
found_address = NULL;
success = search(2, 10, array, found_address); ....
//why doesn't this work
bool search(int value, int size, const int array[], const int*l)
{
int i = 0;
for(i = 0; i<size;i++)
{
if(array == value)
{
l = &array;
return true;
}
}
return false;
}
my question is: why doesn't my SEARCH function work? I pass a pointer
to SEARCH function and assign it an address. Then when I finish the
call, the memory address should be save in the pointer, but it
doesn't. How do I make it work?


To return a pointer to int through function arguments, you must make
it 'int**' in arg list:
bool search(int value, int size, const int array[], const
int**location);
^^
In the function:
*l = &array;
When calling:
success = search(2, 10, array, &found_address);
 
J

Jason

Hello. I am trying to learn how pointer works because I want to write
some functions that can return multiple values. Here is my code:
...
bool search(int value, int size, const int array[], const
int*location);

int main(int argc, char *argv[])
{
bool success = false;
const int array[10] = {1,2,3,4,5,6,7,8,9,10};
const int *found_address = NULL;
...
found_address = NULL;
success = search(2, 10, array, found_address); ...
//why doesn't this work
bool search(int value, int size, const int array[], const int*l)
{
int i = 0;
for(i = 0; i<size;i++)
{
if(array == value)
{
l = &array;
return true;
}
}
return false;
}
my question is: why doesn't my SEARCH function work? I pass a pointer
to SEARCH function and assign it an address. Then when I finish the
call, the memory address should be save in the pointer, but it
doesn't. How do I make it work?


To return a pointer to int through function arguments, you must make
it 'int**' in arg list:
bool search(int value, int size, const int array[], const
int**location);
^^
In the function:
*l = &array;
When calling:
success = search(2, 10, array, &found_address);



I like to thank everyone for your info and help. I realized my error.

Let me summarize what I learned: if I want to return a value through
reference in a function all, I need to add 1 more level of reference
to it. For example if I want **found_address to get the address
location of a 2D array, then I'll use the following:

bool search(int value, int size, const int**array, const
int***location)

Did I get it right this time?
 
J

John Harrison

I like to thank everyone for your info and help. I realized my error.

Let me summarize what I learned: if I want to return a value through
reference in a function all, I need to add 1 more level of reference
to it. For example if I want **found_address to get the address
location of a 2D array, then I'll use the following:

bool search(int value, int size, const int**array, const
int***location)

Did I get it right this time?

You have a gained deeper understanding, grasshopper.

john
 

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

Forum statistics

Threads
474,160
Messages
2,570,890
Members
47,423
Latest member
henerygril

Latest Threads

Top