• Advertisement
Sign in to follow this  

Passing a pointer in C

This topic is 4474 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This should be easy stuff for you guys. Myself, I'm new to C... (sort of) What I'm trying to do is implement a linked list.
struct ALinkedList
{
    char* word;
    struct ALinkedList* next;
};

typedef struct ALinkedList* LinkedList;
I'll choose a very simple function. A function that initializes a list to null.
void initializeLL(LinkedList list)
{
     list = NULL;
}
int main()
{
     LinkedList list;
     initializeLL(list);
     if(list == NULL)
          printf("stuff");
     return 0;
}
It does not print "stuff". I thought if you passed a pointer to a function, it is automatically by reference. What's going on?

Share this post


Link to post
Share on other sites
Advertisement
Pointers are not automatically passed by reference. They can be USED AS references to the items they point to.

Share this post


Link to post
Share on other sites
If you pass a pointer to a function the pointer is passed by value. What you seem to want to do is to pass a pointer to the pointer, i.e. a LinkedList *, and dereference that pointer.

void initializeLL(LinkedList * list)
{
*list = NULL;
}

Share this post


Link to post
Share on other sites
Err...maybe i'm still not understanding. LinkedList is a pointer to a structure already. If i pass a pointer through the function, it's passing the address of the data i want to change. So it is giving me reference to what i want to change...no?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by MrSaturnFan
Err...maybe i'm still not understanding. LinkedList is a pointer to a structure already. If i pass a pointer through the function, it's passing the address of the data i want to change. So it is giving me reference to what i want to change...no?


No. You have a pointer (to struct ALinkedList) in main(), which points off to nowhere. You want the pointer in main to point to NULL. When you pass the pointer to the function, a copy of the pointer itself is made. The copy is set to null. The original is untouched.

By passing a pointer *to that pointer*, you tell the function "here is a location in memory where an ALinkedList* resides", and the function can in turn place a NULL at that location in memory - thus modifying the pointer that the main() stack frame has.

Confusing? Yes. Unexpressive to write a double-pointer when one level of pointing means "I have a linked list somewhere" and the other means "pass the pointer in by reference"? Yes. This is why in C++, you should not do things this way, but instead "use references when you can; use pointers when you have to". (Of course, in C++, you should generally not implement your own linked list, but use the one provided in the standard library instead.) In C, you need to grin and bear it (and comment appropriately).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
lol, i can't even log in but...thanks. I THINK i understand. Why THINK? Because things are working out to my understanding right now :P

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Hey, looks like my linked list is working. Great :)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement