程序 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;
}
程序B - 假设已经创建了一个链表,first(声明为全局变量)指向链表中的第一个节点。
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;
}
我的疑问是,在程序A中调用 create 函数时为什么需要&运算符,而在程序B中调用 display 函数时却不需要。两个函数都将指针作为参数。您能否通过示例解释在调用函数时& 和* 运算符之间的关系。谢谢。
&
。如果您已经拥有正确类型的指针,则不使用&
。 - Jonathan Leffler&x
allows to get the address of x whatever its usage, if you need to get the value of x usex
if you need to get its address use&x
- bruno*first
。 - Weather Vane&
作为参数
发送到任何函数的参数
时,应该有一个*
来保存那个&
。这是纯粹的关系。 - Shubham