指向列表中指针赋值的指针

时间:2022-09-12 23:24:45
#include <stdio.h> 
#include <stdlib.h>
#define ROZ 100
struct lista
{
    char *imie ;
    char *nazwisko;
    long int data;
    struct wsk * next;
};

int add (struct lista** head)
{
    struct lista *wsk;
    wsk=(struct lista*)malloc(sizeof(struct lista));
    long int data;
    wsk->imie=(char*)malloc(ROZ);
    if(wsk-> imie==NULL)
        return -1;
    wsk->nazwisko=(char*)malloc(ROZ);
    if(wsk->nazwisko==NULL)
        return -2;
    printf("podaj imie");
    fgets(wsk->imie,ROZ,stdin);
    printf("podaj nazwisko");
    fgets(wsk->nazwisko, ROZ, stdin);
    printf("podaj date urodzenia");
    scanf("%ld",&data);
    wsk->data=data;
    wsk->next=(*head);
    (*head)=wsk;
}



main()
{
    struct lista *head, *wsk;
    int a, spr;
    while(a!=4)
    {
        printf("Aby wypisac liste wpisz 1, aby dodac wiersz wpisz 2, aby usunac wiersz wpisz 3, aby zakonczyc prace programu wpisz 4");
        scanf("%d",&a);
        if(a==2)
        {
            spr=add(&head);
            {
                if(spr<0)
                    printf("blad");
            }
        }

    }
}

I don't know how am I suppose to move the head of the list(29 line), program doesn't compile there. I would really appreciate an explanation. I have nothing to add, but I have to add some more details so ignore this sentence.

我不知道我怎么想移动列表的头部(29行),程序不在那里编译。我真的很感激解释。我没有什么可补充的,但我必须添加更多细节,所以忽略这句话。

1 个解决方案

#1


2  

struct lista
{
    char *imie ;
    char *nazwisko;
    long int data;
    struct wsk * next;
};

Here,just change the wsk to lista and also add

在这里,只需将wsk更改为lista并添加即可

return 0; 

at the end of the function add.

在函数末尾添加。

Also,when you enter the while loop for the first time,a isn't initialized and it contains "Garbage values". So,the condition may/may not be true. To fix it,just change the while loop into a do...while loop as this loop executes at least once before the condition is checked.

此外,当您第一次进入while循环时,a未初始化并包含“Garbage values”。因此,条件可能/可能不是真的。要修复它,只需将while循环更改为do ... while循环,因为此循环在检查条件之前至少执行一次。

It is also a good practice to check the return values of scanf,malloc etc to see if they were successful.

检查scanf,malloc等的返回值以查看它们是否成功也是一种很好的做法。

#1


2  

struct lista
{
    char *imie ;
    char *nazwisko;
    long int data;
    struct wsk * next;
};

Here,just change the wsk to lista and also add

在这里,只需将wsk更改为lista并添加即可

return 0; 

at the end of the function add.

在函数末尾添加。

Also,when you enter the while loop for the first time,a isn't initialized and it contains "Garbage values". So,the condition may/may not be true. To fix it,just change the while loop into a do...while loop as this loop executes at least once before the condition is checked.

此外,当您第一次进入while循环时,a未初始化并包含“Garbage values”。因此,条件可能/可能不是真的。要修复它,只需将while循环更改为do ... while循环,因为此循环在检查条件之前至少执行一次。

It is also a good practice to check the return values of scanf,malloc etc to see if they were successful.

检查scanf,malloc等的返回值以查看它们是否成功也是一种很好的做法。