Thread: problem in link list C

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    17

    problem in link list C

    this is my struct
    Code:
    struct student{
    
    
        char studentFirstName[40];
        char studentLastName[40];
        int studentAge;
        int studentRollNo;
        char studentClassSection[2];
        int studentTotalNumber;
        char studentPromotion[5];
        struct student* nextPoint;
    };
    and here is my simple function that i use to insert data into link list but when i do in this way
    Code:
    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; here give me an errore expression must be a modifiable lvalue

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You can't use assignment with C-strings, you need to use strcpy().


    Jim

  3. #3
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    When I have a large struct like that, that takes a lot of parameters in its constructor (for lack of a better word) I tend to use the struct itself as an argument, this also makes expanding the structure easier as you don't have to remember to add the new parameter to your prototypes and the definition of the function.

    Example:
    Code:
    struct student{
        char studentFirstName[40];
        char studentLastName[40];
        int studentAge;
        int studentRollNo;
        char studentClassSection[2];
        int studentTotalNumber;
        char studentPromotion[5];
        struct student* nextPoint;
    };
    
    struct student * new_student(struct student * opt) /* <-- structure itself is the argument list */
    {
        struct student * ns;
    
        if (ns = malloc(sizeof *ns)) {
            strncpy(ns->studentFirstName, opt->studentFirstName, sizeof ns->studentFirstName);
            /* and so on ... */
        }
    
        return ns;
    }
    
    int main(void)
    {
        struct student opt = {
            .studentFirstName = "Whatever",
            /* ... */
        };
        struct student * node;
    
    
        node = new_student(&opt);
        
        /* ... error check and do stuff with node ... */
    
        return 0;
    }
    Now if I add or remove items from the struct student object I don't have to change my parameter list. One other HUGE benefit of this is you can now also use this function to COPY an existing struct student. Simply pass the existing struct you want to copy as the argument to new_student() and you will get a copy of that student in return.
    Last edited by nonpuz; 10-05-2013 at 02:17 PM.

  4. #4
    Registered User
    Join Date
    Aug 2013
    Posts
    17
    if i do in this way where is my mistake thanks very much

    this is my global struct pointer
    struct student* tempNextPoint = NULL;

    i create this function to insert data into link list
    Code:
    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;
    }
    and with this function i display all data but in output ity give me all the same data for all student in link list why ?
    Code:
    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");
    }
    and in my main i do this way
    Code:
    int studentNumber,num;
        struct student *getStudentInfo;
    
    
        getStudentInfo = (struct student*)malloc(sizeof(struct student));
    
    
        printf("\t\t\n for how many student you want do this opration -> ");
        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);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List problem
    By peterderijp in forum C Programming
    Replies: 1
    Last Post: 11-05-2010, 11:49 AM
  2. Link List problem
    By Darkinyuasha1 in forum C Programming
    Replies: 2
    Last Post: 05-08-2009, 07:47 PM
  3. Problem in link list
    By Bargi in forum C Programming
    Replies: 2
    Last Post: 07-16-2008, 01:54 AM
  4. Problem with Link List
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 04-24-2002, 11:49 PM
  5. Link list and node problem
    By tom_mk in forum C++ Programming
    Replies: 0
    Last Post: 02-21-2002, 05:18 PM