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?
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.
Just like any other argument, pointers can also be passed to a function as an argument.
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)++; .
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.
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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With