I have got this question on my c++ homework.
Write and test the function location that takes, as shown below, a table of pointers to integers p, the size of such a table n and an integer value x.
int* location (int* p [ ], int n, int x);location searches the set of integers pointed at by the table of pointers p for a match to the value of x. If a matching integer is found, then location returns the address of that integer, NULL otherwise.
I'm not sure that I fully understand the question. However, I tried to solve it but I got error(the program crashes). Here is my code.
#include<iostream>
using namespace std;
int* location (int* p [ ], int n, int x);
void main(){
int arr[3]={1,2,3};
int *ptr=arr;
int *address= location (&ptr, 3, 2);
cout<<&arr[3]<<" should be equal to "<<address<<endl;
}
int* location (int* p [ ], int n, int x){
for(int i=0; i<n; i++){
if(*p[i]==x){return p[i];}
}
return NULL;
}
Can someone please show me my mistake or tell me if I'm solving the question correctly?
Thanks
return NULL;.*(p[i])or(*p)[i]to make your code clear. (According to the description, you want the first)