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?
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?