Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pointers as arguments in C functions

In a lot of examples I've read a simple getListLength() function would look something like this:

int getListLength(struct node *head)
{
    struct node *temp = head;
    int iCount = 0;

    while (temp)
    {
        ++iCount;
        temp = temp->next;
    }

    return iCount;
}

What strikes me as unneeded is the declaration of a local pointer (in this case *temp) that copies the passed parameter. If I recall correctly, passed parameters obtain their own copies. Thus, there won't be a need for a local pointer that copies the *head just because the *head is a copy itself, right? In other words, would it be correct to discard the *temp pointer and use head everywhere instead?

like image 510
jrhetf4xb Avatar asked Jan 05 '13 20:01

jrhetf4xb


People also ask

How can a function pointer be an argument?

Pass-by-pointer means to pass a pointer argument in the calling function to the corresponding formal parameter of the called function. The called function can modify the value of the variable to which the pointer argument points. When you use pass-by-pointer, a copy of the pointer is passed to the function.

Can we pass pointer as an argument to function?

Just like any other argument, pointers can also be passed to a function as an argument.

How can we pass a pointer as an argument to a function explain with example?

Example 2: Passing Pointers to Functions Here, the value stored at p , *p , is 10 initially. We then passed the pointer p to the addOne() function. The ptr pointer gets this address in the addOne() function. Inside the function, we increased the value stored at ptr by 1 using (*ptr)++; .

Can we use pointers for functions C?

In C, like normal data pointers (int *, char *, etc), we can have pointers to functions. Following is a simple example that shows declaration and function call using function pointer.


3 Answers

Yes, it's a copy, so yes, it would be correct.

int getListLength(struct node* head)
{
    int iCount = 0;

    while (head)
    {
        ++iCount;
        head = head->next;
    }
    return iCount;
}

Why don't you execute it and see for yourself?

like image 68
cmc Avatar answered Oct 12 '22 05:10

cmc


While it's true that you don't need the local copy since the pointer is passed by value, it's probably there for stylistic reasons. Some consider it bad form to modify arguments passed in (though I do find it useful in some scenarios), but perhaps more importantly, you lose some of the self-documentation in the code; specifically, head no longer always points to the true head of the linked list. This isn't that confusing in your short piece of code, but having inaccurately-named variables can be much more confusing when the code is longer and more complex.

like image 26
jjlin Avatar answered Oct 12 '22 05:10

jjlin


Often, the reason to make a local copy of a passed-in pointer is to reduce the side-effects of a function (by not modifying the function parameter).

If a function is only using the pointer to read (not write), and has no other interaction with the outside world, the function could be annotated as 'pure' in GCC and would be open for some nice optimizations.

Example:

__attribute__((pure)) int getListLength(struct node *head) 
{
    struct node *temp = head;
    int iCount = 0;

    while (temp)
    {
        ++iCount;
        temp = temp->next;
    }

    return iCount;
}

If you aren't familiar with what side effects are, try reading the Side Effects and Functional Programming Wikipedia articles to get more information on the subject.

like image 20
Philip Conrad Avatar answered Oct 12 '22 05:10

Philip Conrad