0

I can store everything just fine. But I run into problems when implementing an array within a linked list. The baratok is supposed to hold number values until the the input given is -1. But when I try to print it, it only gives me -1. So what could be a proper implementation for this?

My code:

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

int getl(char s[], int lim) 
{
int c,i;
for(i = 0; i < lim && (c = getchar()) != '\n' && c!=EOF; i++)
s[i] = c;
s[i] = '\0'; 
while(c != '\n' && c!=EOF)
c=getchar(); 
return i;
}

struct szemelylista {

    int szemelyId;
    char nev[50];
    int *baratok;
    struct szemelylista *kov;
} ;

int main()
{
    struct szemelylista *llist = NULL, *elozo, *horgony;
    int ct = 0, barat, i;
    char s[50];

    horgony = elozo = NULL;
    while(printf("Name: ") && getl(s, 50))
    {
        if(!(llist = (struct szemelylista*)malloc(sizeof(struct szemelylista)))) { 
            break;
        }


        if(horgony) {
            elozo ->kov = llist;
        } else {
            horgony = llist;
        }
        elozo = llist;

        llist -> kov = NULL;


        llist->szemelyId = ct;

        strcpy(llist->nev, s);

        printf("Friends: "); //this is the problematic part I guess
        getl(s, 50);
        barat = atoi(s);
        llist->baratok = barat;
        while(barat != -1) {
        getl(s, 50);
        barat = atoi(s);
        llist->baratok = barat; //until here
        }

        ct++;
    }

    llist = horgony;

    printf("ID\Name\Friends\tFriends with text\n");
    printf("--------------------------------------------\n");
    while(llist) {

        printf("\t%d\t%s\t%d\t\n", llist->szemelyId, llist->nev, llist->baratok);
        llist = llist->kov;

    }

}

1 Answer 1

4

You can't just assign multiple times to a single pointer and somehow think that multiple values got stored. It doesn't work like that. Each assignment completely overwrites the previous value in that variable.

You need to actually manage the array, either by making it a fixed-size array:

int baratok[100]; /* Max 100 numbers, no idea if this is sensible for you. */

Or by allocating a dynamic array on the heap (using malloc()) and growing it using realloc() as more space is needed).

In either case, you're going to have to add another variable that keeps track of how many numbers have been added, up to the current allocation maximum.

Memory management is a bit more complicated than you seem to expect, you need to read more basic C reference material.

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

1 Comment

Thanks man, the array one worked. However, when I print it it fills up with 0's the rest (sizeof(llist->baratok)/sizeof(llist->baratok[0]). So how can I actually print the numbers within each loop to match with the containing numbers?

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.