1
#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.

6
  • 2
    struct lista *head; head is not initialised. Commented Jan 11, 2015 at 14:13
  • "program doesn't compile there" - If it does not compile,could you include the error(s) and/or warning(s) that you get in your post? Commented Jan 11, 2015 at 14:16
  • int a, spr; while(a!=4){...}- Here,a isn't initialized. Commented Jan 11, 2015 at 14:17
  • Perhaps if your program knew what a struct wsk was (which is not what a struct lista is) at least one of your errors would be more obvious. Commented Jan 11, 2015 at 14:18
  • 1
    That's because there is no struct wsk type. Commented Jan 11, 2015 at 14:19

1 Answer 1

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

Here,just change the wsk to lista and also add

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.

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.