PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, July 6, 2022

[FIXED] when should we add '&' and when we shouldn't while calling a function in c language?

 July 06, 2022     c, pass-by-reference, pass-by-value, pointers, struct     No comments   

Issue

Program A

void create(struct Stack *st) 
{
    printf("Enter Size");
    scanf("%d",&st->size);
    st->top=-1;
    st->S=(int *)malloc(st->size*sizeof(int));
}

int main()
{
struct stack st;
create(&st);
return 0;
}

Program B - for program B assume that a linked list has been created with first(declared as a global variable) pointing at the 1st node in the list.

struct Node 
{ 
 int data;
 struct Node *next;
}*first=NULL;

void display(struct Node *p) 
{
while(p!=NULL)
{
 printf("%d ",p->data);
 p=p->next;
}
} 

int main()
{
display(first);
return 0;
}

My doubt is, why in program A while calling the create function, the & operator is required and why in program B it is not used while calling display function. Both create and display function take pointer as argument. Can you explain the relationship between & and * operator while calling a function with examples. Thanks in advance.


Solution

When you send some & as arguments to any function's parameters, there should be * to hold that & or if there's any * in function's parameters you can send arguments like & or *. This is the mere relationship.

In Program A:

void create(struct Stack *st) 
{
    printf("Enter Size");
    scanf("%d",&st->size);
    st->top=-1;
    st->S=(int *)malloc(st->size*sizeof(int));
}

int main()
{
struct stack st;
create(&st);
return 0;

You need to send &st because sending &st to create() provides you the access to the memory where st is stored. If you send st--which is just a name of the memory location, where st's data is stored--as an argument to create() that would be merely copying the st into struct Stack *st and result into an error. And you cannot modify the original value using its copy as in the case of scanf("%d", &st->size); that's why in the first program you need to send the address of st.

In Program B:

struct Node 
{ 
 int data;
 struct Node *next;
}*first=NULL;

void display(struct Node *p) 
{
while(p!=NULL)
{
 printf("%d ",p->data);
 p=p->next;
}
} 

int main()
{
display(first);
return 0;
}

You have already the memory address where data of struct Node type is stored, i.e. the value of first. That's why you don't need to do display(&first) in this case just make a copy of first and use it in display() function.

Can you explain the relationship between & and * operator while calling a function with examples.

But you can also do display(&first) in Program B like this:

struct Node 
{ 
 int data;
 struct Node *next;
}*first=NULL;

void display(struct Node **p) 
{
while(*p!=NULL)
{
 printf("%d ",(*p)->data);
 *p=(*p)->next;
}
} 

int main()
{
display(&first);
return 0;
}

and I hope the example given above makes it clear when to use * and & while calling any function, according to its parameters. Beware, displaying data using &first would modify the address of first in *p=(*p)->next; and your head pointer to the linked list will be lost so, this example is just for demonstration purposes.



Answered By - Shubham
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,217,192

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing