Thread: Problem with linked list

  1. #1
    Registered User
    Join Date
    Jan 2018
    Posts
    5

    Problem with linked list

    Hi all, i have problem with linked list.
    This is the code

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define N 100
    
    
    struct Utenti{
    char nome [20];
    char cognome [20];
    char username [20];
    char email [20];
    };
    
    
    
    
    struct Profilo{
    char username [20];
    char email [20];
    char titolo_video [20];
    int durata_video;
    int dimensione_video;
    };
    
    
    
    
    struct L_utenti{
    char nome;
    char cognome;
    char nick;
    char email;
    struct nodo *next;
    };
    
    
    struct L_utenti *testa = NULL;
    struct L_utenti *nodo;
    
    
    struct Profilo profilo [N];
    struct Utenti utenti [N];
    
    
    
    
    void Lista_utenti(FILE *ptr_archivio);
    
    
    
    
    int main (){
    
    
    FILE *ptr_archivio;
    
    
    ptr_archivio = fopen("Archivio.txt" , "r");
    
    
    Lista_utenti(ptr_archivio);
    
    
    return 0;
    
    
    }
    
    
    void Lista_utenti(FILE *ptr_archivio){
    
    
    char archivio_array [N];
    int i=0;
    
    
    if (ptr_archivio == NULL){
        printf("Errore, file non trovato!");
    }
    else {
    
    
    while(fgets(archivio_array, N, ptr_archivio)!= NULL){
        sscanf(archivio_array, "%s %s %s %s", utenti[i].nome, utenti[i].cognome, utenti[i].username, utenti[i].email);
        printf("Nome: %s\n" , utenti[i].nome);
        printf("Cognome: %s\n" , utenti[i].cognome);
        printf("Username: %s\n" , utenti[i].username);
        printf("Email: %s\n" , utenti[i].email);
        printf("\n\n");
        i++;
        }
        fclose(ptr_archivio);
    }
    
    
    
    
    
    
    
    
    for (i=0;i<3;i++){
    nodo = malloc(sizeof(struct L_utenti));
    nodo->nome = utenti[i].nome;
    nodo->cognome = utenti[i].cognome;
    nodo->nick = utenti[i].username;
    nodo->email = utenti[i].email;
    nodo->next=
    testa;
    testa=nodo;
    
    
    
    
    }
    
    
    while(testa!=NULL){
        printf("%s-->",testa->nome);
        printf("%s-->", testa->cognome);
        printf("%s-->", testa->nick);
        printf("%s-->", testa->email);
        testa=testa->next;
        }
        printf("NULL\n");
    
    
    }
    I have to create a linked list with the name,surname and username members, but the problem is that in the while condition the program crash.
    Another question there is a method to read the file and put it into a linked list? whitout using a structure and then passing data in a linked list.

    Sorry if my code isn't good but i'm a beginner

  2. #2
    Registered User
    Join Date
    Jan 2018
    Posts
    2
    One of your problems seems to be that the datatype 'struct L_utenti' has members of type 'char' instead of 'char*'.

    This leads to problems in the second to last loop where you want to copy the adress of each item into a pointer to character but due to only using a character variable you're truncating the adress instead.
    In the last loop('while(testa!=NULL) ... ') you feed these truncated pseudo adresses, stored in the char variables, to 'printf' and it rightfully implodes.

    This is the most obvious unsound issue with this code - there may be much more of course.

    You compiler was most likely issuing warnings aplenty... did you ignore them?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list problem
    By Carlrobertoh in forum C Programming
    Replies: 0
    Last Post: 11-05-2015, 12:17 PM
  2. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  3. linked list problem
    By cathzee in forum C Programming
    Replies: 1
    Last Post: 11-07-2003, 08:11 AM
  4. Linked list problem....
    By davidvoyage200 in forum C++ Programming
    Replies: 2
    Last Post: 03-07-2003, 10:37 PM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM

Tags for this Thread