Sign in to follow this  
MrSaturnFan

Passing a pointer in C

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this