Search code examples
c++pointersstructdereference

How do I dereference the address returned by a pointer from a function that takes an int & a pointer of type struct?


I am passing an int & a pointer to a struct into a function that returns a pointer to data in the struct. How do I dereference the address returned by the pointer?

i have created a search function to look for a node in a linked list. this is my search function:

Entry* search(int num, Entry* &head)
{
    Entry *current;

    current = head;

    while(current != NULL)
    {
        if(atoi(current->number) == num)
        {
            return(current); //selected node is found
        }
        current = current->next; //move to next node to check for num
    }

    return head;//if not found return head of linked list
}

My problem is in trying to get the value for the pointer returned from:

cout << (search(searchNum, listHead));

the statement above returns the address to the value returned by the pointer in the function like this:

0x100300000

so i tried different variations of something like this:

cout << *(search(searchNum, listHead));

but i only get errors....

can anyone help me figure out how to get the value from the pointer being returned?

ultimately, id like to finish it off with something like this:

found = search(searchNum, listHead);
cout << "The number you searched for is: " << *found;

but it just aint happening :/

thanks guys. I'm a newbie so please forgive me for being completely off base on any of this... i tried to search for a similar question but just could not get any clear answer... maybe its true what joel spolsky said about pointers... some people are missing a part in their brains that makes them understand them... :/


Solution

  • Try:

    cout << "The number you searched for is: " << found->number;
    

    or

    cout << "The number you searched for is: " << (*found).number;