Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does passing "pointer to structure" to a function create local copies of it in C?

Tags:

c

pointers

struct

I have a structure like this

struct node
{
    int data;
    struct node* next;
};

Which I use to create singly linked list.

I created other functions like

int push(struct node* head,int element);

which pushes data onto stack created using node structs.

The function then tries to update the struct node* head passed to it using code(it does other things as well)

head=(struct node*)malloc(sizeof(struct node));

The call is made as such

struct node* stack;
push(stack,number);

It looks like this code created copy of the pointer passed to it. So I had to change the function to

int push(struct node** head,int element)

and

*head=(struct node*)malloc(sizeof(struct node));

The call is made as such

struct node* stack;
push(&stack,number);

So my question is, what was the earlier function doing? Is it necessary to pass struct node** to the function if I want to update original value of pointer or is my approach wrong?

Sorry I cannot provide complete code as it is an assignment.

like image 829
Registered User Avatar asked Sep 22 '15 11:09

Registered User


People also ask

Does passing a pointer make a copy?

If you declare a formal parameter of a function as a pointer type, you are passing that parameter by its address. The pointer is copied, but not the data it points to.

What happens when you pass a pointer to a function in C?

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.

What happens if you pass a pointer to a function?

When we pass a pointer as an argument instead of a variable then the address of the variable is passed instead of the value. So any change made by the function using the pointer is permanently made at the address of passed variable. This technique is known as call by reference in C.

Can we pass pointer to a structure to a function?

The address of the structure is passed as an argument to the function. It is collected in a pointer to structure in function header.


3 Answers

C always passes by value. To change a variable passed to a function, instead of passing the variable itself, you pass a reference(its address).

Let's say you're calling your function with the old signature

int push(struct node* head,int element);

struct node *actual_head = NULL;
push(actual_head, 3);

Now before calling push, your variable actual_head will have value as NULL.

Inside the push function, a new variable head will be pushed to stack. It will have the same value as passed to it, i.e. NULL.

Then when you call head = malloc(...), your variable head will get a new value instead of actual_head which you wanted to.

To mitigate the above, you'll have to change the signature of your function to

int push(struct node** head,int element);

struct node *actual_head = NULL
push(&actual_head, 3);

Now if you notice carefully, the value of actual_head is NULL, but this pointer is also stored somewhere, that somewhere is its address &actual_head. Let's take this address as 1234.

Now inside the push function, your variable head which can hold the address of a pointer(Notice the two *), will have the value of 1234

Now when you do *head = malloc(...), you're actually changing the value of the object present at location 1234, which is your actual_head object.

like image 178
Kartik Anand Avatar answered Oct 11 '22 03:10

Kartik Anand


C always passes parameters by value (i.e., by copying it). This applies even to pointers, but in that case, it is the pointer itself that is copied. Most of the times you use pointers, that is fine, because you are interested in manipulating the data that is pointed to by the pointer. However, in your situation, you want to modify the pointer itself, so you do indeed have to use a pointer to a pointer.

like image 5
Aasmund Eldhuset Avatar answered Oct 11 '22 02:10

Aasmund Eldhuset


Yes.

The first version of your program was passing the pointer by value. Although it passed an address (held by the pointer to struct) it didn't pass the pointer's address - necessary to update the value.

Whenever you want to update a variable's value you must pass the variable's address. To pass a pointer address, you need a parameter pointer to pointer to type.

In your case, pointer to pointer to struct node.

like image 2
Enzo Ferber Avatar answered Oct 11 '22 01:10

Enzo Ferber