Visualizzazione dei risultati da 1 a 9 su 9

Discussione: problema link list C

Hybrid View

  1. #1

    problema link list C

    questa è la mia struct
    codice:
    struct student{
    
    
    	char studentFirstName[40];
    	char studentLastName[40];
    	int studentAge;
    	int studentRollNo;
    	char studentClassSection[2];
    	int studentTotalNumber;
    	char studentPromotion[5];
    	struct student* nextPoint;
    };
    questa è la mia funzione che uso per inserire tutti dati nella link list
    codice:
    void insertDataIntolinkList(char *stufirstname,char *stulastname,int stuage, int sturollno, char *stuclasssection, int stutotalnumber, char *stupromotion){
    	struct student* tempStudentInfo = (struct student*)malloc(sizeof(student));
    	(*tempStudentInfo).studentFirstName = stufirstname;
    }
    (*tempStudentInfo).studentFirstName = stufirstname qui mi da un errore dice che espressione deve essere di un lvalue modificabile cosa vuol dire grazie mille
    shiviphpdevelopment

  2. #2
    Utente di HTML.it L'avatar di oregon
    Registrato dal
    Jul 2005
    residenza
    Roma
    Messaggi
    36,480
    In C per assegnare una stringa la devi copiare con strcpy ...

    strcpy(tempStudentInfo->studentFirstName, stufirstname);
    No MP tecnici (non rispondo nemmeno!), usa il forum.

  3. #3
    io sto faccendo cosi ma non so cosa sbaglio come output mi da tutti dati uguali grazie mille
    ho creato questa funzione per inserimento

    struct student* tempNextPoint = NULL; ho creato questo puntatore globale e inizializzo null

    codice:
    void insertDataIntolinkList(char *stufirstname,char *stulastname,int stuage, int sturollno, char *stuclasssection, int stutotalnumber, char *stupromotion){
        struct student* tempStudentInfo = (struct student*)malloc(sizeof(student));
        
        strcpy((*tempStudentInfo).studentFirstName,stufirstname);
        strcpy((*tempStudentInfo).studentLastName,stulastname);
        (*tempStudentInfo).studentAge = stuage;
        (*tempStudentInfo).studentRollNo = sturollno;
        strcpy((*tempStudentInfo).studentClassSection,stuclasssection);
        (*tempStudentInfo).studentTotalNumber = stutotalnumber;
        strcpy((*tempStudentInfo).studentPromotion,stupromotion);
    
    
        (*tempStudentInfo).nextPoint = tempNextPoint;
        tempNextPoint = tempStudentInfo;
    }
    e con questa funzione faccio print di tutti dati ma in output sono tutti uguali anche se inserisco dati diversi perchè
    codice:
    void displayAllStudentResult(){
        struct student* tempPrintPoint = tempNextPoint;
        while(tempPrintPoint != NULL){
            printf("\t\t\n student first name is %s ",(*tempPrintPoint).studentFirstName);
            printf("\t\t\n student last name is %s ",(*tempPrintPoint).studentLastName);
            printf("\t\t\n student age is %d ",(*tempPrintPoint).studentAge);
            printf("\t\t\n student roll number is %d ",(*tempPrintPoint).studentRollNo);
            printf("\t\t\n student class section is %c ",(*tempPrintPoint).studentClassSection);
            printf("\t\t\n student total number %d ",(*tempPrintPoint).studentTotalNumber);
            printf("\t\t\n student promotion %s ",(*tempPrintPoint).studentPromotion);
    
    
            tempPrintPoint = (*tempPrintPoint).nextPoint;
        }
    
    
        printf("\n");
    }
    nel main sto faccendo in questo modo
    codice:
    int studentNumber,num;
        struct student *getStudentInfo;
    
    
        getStudentInfo = (struct student*)malloc(sizeof(struct student));
    
    
        printf("\t\t\n quanti studenti -> ");
        scanf("%d",&studentNumber);
    
    
        for(num = 1; num <= studentNumber; num++ ){
    
    
        printf("\t\t\n enter student firstname (max 40 charecters ) ");
        scanf("%s",&(getStudentInfo)->studentFirstName);
    
    
        printf("\t\t\n enter student lastname (max 40 charecters ) ");
        scanf("%s",&(getStudentInfo)->studentLastName);
    
    
        printf("\t\t\n enter student age ");
        scanf("%d",&(getStudentInfo)->studentAge);
    
    
        printf("\t\t\n enter student roll number ");
        scanf("%d",&(getStudentInfo)->studentRollNo);
    
    
        printf("\t\t\n enter student class section (A,B,C,D ecc... ) ");
        scanf("%s",&(getStudentInfo)->studentClassSection);
    
    
        printf("\t\t\n enter student total number ");
        scanf("%d",&(getStudentInfo)->studentTotalNumber);
    
    
        printf("\t\t\n enter student promotion result  (pass or fail) ");
        scanf("%s",&(getStudentInfo)->studentPromotion);
    
    
        }
    
    
        insertDataIntolinkList((*getStudentInfo).studentFirstName,(*getStudentInfo).studentLastName,(*getStudentInfo).studentAge,(*getStudentInfo).studentRollNo,(*getStudentInfo).studentClassSection,(*getStudentInfo).studentTotalNumber,(*getStudentInfo).studentPromotion);
    
    
        for(num=0;num<=studentNumber;num++){
            displayAllStudentResult();
        }
    
    
        free(getStudentInfo);
    Ultima modifica di shivi_php; 06-10-2013 a 11:38
    shiviphpdevelopment

  4. #4
    Semplicemente perché inserisci solo le informazioni una volta uscito dal loop, di conseguenza inserirai solo le informazioni dell'ultimo studente inserito nella lista.


    Nota che non effettui la de-allocazione della memoria occupata dalla lista.

  5. #5
    ma de-allocazione della memoria devo fare anche nelle funzioni ? perchè nel main io ho fatto de-allocazione di memoria .
    mi puoi dare un picciolo esempio cosa devo cambiare nel mio codice durante inserimento e poi output grazie mille
    shiviphpdevelopment

  6. #6
    io de-allocazione faccio nella funzione displayAllStudentResult() e nella mia funzione main addesso funziona bene e non mi da nessun errore e mi da tutti dati di ogni studente
    shiviphpdevelopment

  7. #7
    Io l'unica free che vedo è quella relativa a getStudentInfo che rappresenta un nodo e non l'intera lista. Per de-allocare la struttura dati devi de-allocare ogni singolo nodo. In pratica alla fine del main dovrai scorrere tutta la lista e de-allocare man mano ogni singolo elemento.

  8. #8
    ah ok ho risolto grazie mille
    shiviphpdevelopment

  9. #9
    cmq non avevo postato io nella mia funzione displayAllStudentResult()
    io faccio de-allocazione cosi free(tempPrintPoint);
    shiviphpdevelopment

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.