0

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 momment:

    #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;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
Linas
  • 31
  • 2
  • 1
    You are not putting any data in `anker`. You could say something like `anker->nummer = 5` and `anker->name="test"` and then you will see some output – edtheprogrammerguy Apr 12 '23 at 13:31
  • I suggest you create functions to add nodes to the top of the list, print one single node from the list, and print all nodes in the list. To help with that, take some paper and a pencil, and draw down all the operations you need to do. Use small labeled boxes for the nodes, and arrows for all pointers and links. Erase and redraw arrows as you do your operations on them. Once you get that to work start making code to follow what you're come up with on the paper. – Some programmer dude Apr 12 '23 at 13:33
  • A 'video' is rarely a very good teacher. Try getting yourself a good book on C. Additionally: You should be getting warnings. Respect them. – Refugnic Eternium Apr 12 '23 at 13:33
  • Your forgot `#include ` – Jabberwocky Apr 12 '23 at 13:44
  • The `main` function does not call any functions. – ryyker Apr 12 '23 at 13:45
  • I suggest you get familiar with the basics of the C language first. – Jabberwocky Apr 12 '23 at 13:47

2 Answers2

1

"...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);
    }
    
    
ryyker
  • 22,849
  • 3
  • 43
  • 87
0

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)
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335