pointers

Hello,

I have this function

1
2
3
4
5
6
7
8
9
10
11
12
13
int *find(int *arr, int numElemns, int *value) 
{
    
    for (int i =0; i < numElemns; i++) // function to find an element in an array using linear search
    {
        if (arr[i] == *value)
        {
            return &arr[i];
        }
     }
     return NULL;
       
}

I'm kind of new to pointers and it's a bit confusing. If I wanted to return a pointer to value, how would I do it?

would the definition be like this?
int *find(int *arr, int numElemns, int *value)
How would I go from there?

I appreciate any feedback.
Last edited on
Yes, that definition would work.

At line 8, either return arr+i; or return &arr[i];

Note: You probably want to move line 12 to after line 14, otherwise you're going to exit the loop on the first comparison, regardless of the number of elements.

The convention for the not found condition with pointers is: return NULL;
I edited the original post just like you said. This is how I'm calling it:

1
2
3
4
cout << "Please enter an integer to search for: ";
            cin >> integer;
            cout << endl;
            find(numbers, arraysize, &integer);

numbers is a dynamic array. It still shows nothing. Thanks a lot for the response.
Last edited on
any help :)?
Should work just fine, what exactly is the problem?

EDIT: Just going to ask, have you tried printing the return value? (I would say to dereference it first but you'd need to check if it's null or not before that to avoid exceptions).
Last edited on
here's my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
int *createArray(int &size) // function to create a dynamic array of random elements
{
    int *array1;
    array1 = new int[size];
    srand(time(0));
    
    for (int i = 0; i < size; i++)
    {
        array1[i] = rand() % 20;
    }
    return array1;
}
int *find(int *arr, int size, int *value) 
{
    
    for (int i =0; i < size; i++) // function to find an element in an array using linear search
    {
        if (arr[i] == *value)
        {
            return &arr[i];
        }
    }
    return NULL;
    
}

int main()
{
    int *numbers;
        int arraysize = 0;
        int integer = 0;
        cout << "please enter an array size: ";
        cin >> arraysize;
        cout << endl << endl;

        numbers = createArray(arraysize);
        cout << "The elements of the array are: ";
        for (int i = 0; i < arraysize; i++)
        {
            cout << numbers[i] << " ";
        }
        cout << endl << endl;
        cout << "enter an integer: ";
        cin >> integer;
        cout << endl << endl;
        find(numbers, arraysize, &integer);

        system("pause");
        return 0;
}
        


When I call the function find, it displays nothing. Thanks again.
find doesn't display anything because that's not what it's doing.
Find returns a pointer to an array element, which you do nothing with.
Try
1
2
3
int *found = find(numbers, arraysize, &integer);
if(found)
    cout << *found << endl;
Last edited on
Great. Thanks a lot, Warnis.
Topic archived. No new replies allowed.