链表指针作为函数参数


#include//无头节点 
#include
typedef struct List
{
    int val;
    struct List *next;
}list;
void a(list *pst)
{
    pst=pst->next;
}
int main()
{
    list *head,*last,*p;
    head=NULL;
    int i,n;
    scanf("%d",&n);
    for(i=0;imalloc(sizeof(struct List));
        scanf("%d",&(p->val));
        p->next=head;
        head=p;
    }
    a(head);
    while(head)
    {
        printf("%d ",head->val);
        head=head->next;
    }
}

将头指针传递给函数,函数功能是指针移向下一节点,为什么该函数a没起到作用

1.函数传参时要传地址才行,如果是指针,就在指针前再加一个*,表示指针的地址;
2.用引用&,在函数形参数前加引用符号。
在主函数中不用加取址
3.还有种方法是利用函数的返回值。比如链表就要返回一个链表头,但是这种方法有个缺点,就是不能返回多个参数。

在函数内如果要修改指针的地址,参数就要用指针的指针