Linas
Linas

Reputation: 31

Displaying an element of a linked list

I would like to ask something again about why I am not getting my elements displayed by trying it to put out through a linked list.

I was following one tutorial for this matter but although it seems to me that i done everything as shown in a video it still doesn't work.

So I am trying to display my data from the array feld[N] through my structure student_t.

I know that for a linked list, I need multiple nodes but I wanted just to print out the first one and see if it works.

My code looks like that at the moment:

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h> 
#include <stdlib.h>

#define MAX_NAME_LEN 50
#define N 20

struct student {
    unsigned int nummer;
    char name[MAX_NAME_LEN];
};

typedef struct student student_t;

int element_vergleich(const void* p1, const void* p2)
{
    student_t* sp1, * sp2;
    sp1 = (student_t*)p1;
    sp2 = (student_t*)p2;

    if (sp1->nummer < sp2->nummer)
        return -1;
    else  if (sp1->nummer > sp2->nummer)
        return 1;
    else
        return 0;
}

void sortiere(student_t* f, int n)
{
    qsort(f, n, sizeof(student_t), element_vergleich);
}

struct listelem
{
    struct student;
    struct listelem *link;
};

int main(void)
{
    student_t feld[N] = { {59112, "Peter Lustig"}, {45181, "Fritz Fuchs"}, {38984, "Bibi Blocksberg"},
                          {87191, "Bernd Brot"}, {58731, "Katie Sommer"}, {75442, "Hein Klug"} };
    int n = 7;   // Anzahl benutzter Elemente im Feld

    strcpy(feld[6].name, "Linas Dagys");
    feld[6].nummer = 50846;

    printf("Das Feld wurde fuer %d Elemente deklariert. Davon werden %d Elemente benutzt.\n", N, n);


    struct listelem* anker = NULL;

    anker = (struct listelem*)malloc(sizeof(struct listelem));

    anker->nummer;
    anker->name;
    anker->link = NULL;
    
    printf(" Nummer: %d, Name: %s", anker->nummer, anker->name);

    return 0;
}

Upvotes: 0

Views: 75

Answers (2)

ryyker
ryyker

Reputation: 23226

"...but i wanted just to print out the first one and see if it works."

The members need to contain something before printf if anything is to be printed out.

Make the following changes to your code, starting at memory allocation:

...
struct listelem* anker = malloc(sizeof(* anker));//no need to cast return of malloc
if(anker)//check for success before using
{
    memset(anker, 0, sizeof(* anker));//initialize allocated memory
    //anker = (struct listelem*)malloc(sizeof(struct listelem));

    //assign values to members before calling printf
    anker->nummer = 10;
    strcpy(anker->name, "some name");
    //anker->link = NULL;
    
    printf(" Nummer: %d, Name: %s", anker->nummer, anker->name);
    ...
    //free memory when finished using it
    free(anker);
 }
 ...

(Why I suggested not casting malloc)

And, to output the sorted feld array, insert the following after the printf:

    sortiere(feld, n);
    for(int i=0;i<n;i++)
    {
        printf(" Nummer: %d, Name: %s\n", feld[i].nummer, feld[i].name);
    }
    
    

Upvotes: 1

Vlad from Moscow
Vlad from Moscow

Reputation: 311088

For starters this structure declaration

struct listelem
{
    struct student;
    struct listelem *link;
};

is incorrect. This line

    struct student;

does declare a data member of the structure.

It seems you mean

struct listelem
{
    student_t student;
    struct listelem *link;
};

So at least these statements

anker->nummer;
anker->name;

are invalid.

You could write for example

anker = malloc(sizeof(struct listelem));
anker->student = feld[0];
anker->link = NULL;

 printf(" Nummer: %d, Name: %s\n", anker->student.nummer, anker->student.name)

Upvotes: 0

Related Questions