Thread: help regarding structs

  1. #1
    Registered User
    Join Date
    Dec 2012
    Location
    Fukushima-shi, Fukushima, Japan, Japan
    Posts
    6

    help regarding structs

    i got two structs. and the second struct contains an array of the first struct
    Code:
    typedef struct{
        char studentNumber[MAX];
        char famName[MAX];
        char firstName[MAX];
        char midName[MAX];
        char course[MAX];
        int year;
        int age;
        char gender;
        int final;
    } studentsInfo;
    
    
    typedef struct
    {
        studentsInfo student[MAX];
        int size; //size of all students listed
    }listOfStudents;
    where MAX = 20. i also have a function which adds a student to the list. my problem is how can i make the program store all info i gathered into the first "part/space/value" of the student array?

    the information above maybe lacking because im still not done with the program and it still looks ******. i can futher add details if you ask

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Suppose you have an instance of the 2nd struct named foo.(not a pointer, just a usual instance)

    Code:
    foo.student[0].age = 20;
    This will take the instance foo, inside foo, it will select the first element of student array. Because the 1st element is a struct, it will select the field named age of this struct. It will assign the value 20 to this field.

    Hope this helps
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    Registered User
    Join Date
    Dec 2012
    Location
    Fukushima-shi, Fukushima, Japan, Japan
    Posts
    6
    i got your point
    but in mine, im using, or need to, use pointers

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    One way is to make a constructor to fill in your studentsInfo. This way divides naturally into different source files, where you have one source file to take care of the studentsInfo, etc

    Code:
    // newStudentDefault : construct a default studentsInfo
    studentsInfo newStudentDefault(void);
    
    // newStudent : construct a studentsInfo with the given fields filled out
    studentsInfo newStudent(char *studentNumber, char *famName, char *firstName,
    		char *midName, char *course, int year, int age, char gender, 
    		int final);
    
    // printStudentList : print the list of students
    void printStudentList(listOfStudents lis);
    
    int main(void)
    {
    	// Make a lis which is uninitialized
    	listOfStudents lis;
    	
    	// Fill element list.student index 0 and 1 using the default struct
    	lis.student[0] = newStudentDefault();
    	lis.student[1] = newStudentDefault();
    	
    	// Fill element lis.student[2] using the customized constructor
    	lis.student[2] = newStudent("4567", "Doe", "Jane", "X", "History 101", 
    			1980, 0, 'F', 0);
    	
    	// Set the size member to the proper value
    	lis.size = 3;
    	
    	// Print the list
    	printStudentList(lis);
    	
    	return EXIT_SUCCESS;
    }
    The constructors can simply return a pre-initialized struct of that type

    Code:
    studentsInfo newStudentDefault(void) {
    	return (studentsInfo) {
    		.studentNumber = "1234",
    		.famName = "Doe",
    		.firstName = "John",
    		.midName = "X",
    		.course = "DefaultCourse",
    		.year = 1980,
    		.age = 0,
    		.gender = 'M',
    		.final = 0,
    	};
    }
    
    studentsInfo newStudent(char *studentNumber, char *famName, char *firstName,
    		char *midName, char *course, int year, int age, char gender, 
    		int final) {
    	studentsInfo s = newStudentDefault();
    	strcpy(s.studentNumber, studentNumber);
    	strcpy(s.famName, famName);
    	strcpy(s.firstName, firstName);
    	strcpy(s.midName, midName);
    	strcpy(s.course, course);
    	s.year = year;
    	s.age = age;
    	s.gender = gender;
    	s.final = final;
    	return s;
    }
    
    void printStudentList(listOfStudents lis) {
    	(void)lis;
    	// TODO
    }
    You can also do it with pointers. The only difference is you'd malloc the struct and set the members using the -> notation instead.

  5. #5
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by cipher1285 View Post
    i got your point
    but in mine, im using, or need to, use pointers
    No problem.
    Example1. No pointers.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAX 100
    
    struct student{
        char name[15];
        int age;
    };
    
    typedef struct student student;
    
    struct listStudent{
        student arrayStd[MAX];
        int size;
    };
    
    typedef struct listStudent listStudent;
    
    int main(void)
    {
        int i;
        /* No pointers. Initialize */
        listStudent lStd;
        lStd.size = 0;
        
        /* Create 2 students */
        /* 1st */
        strcpy(lStd.arrayStd[0].name, "Samaras");
        lStd.arrayStd[0].age = 20;
        lStd.size++;
        
        /* 2nd */
        strcpy(lStd.arrayStd[1].name, "Midha");
        lStd.arrayStd[1].age = 21;
        lStd.size++;
        
        for(i = 0 ; i < lStd.size ; i++)
        {
            printf("Student %s is %d years old\n", lStd.arrayStd[i].name, lStd.arrayStd[i].age);
        }
        
        return 0;
    }
    Now for the pointers, you have to understand that in order to access a member of a normal instance of struct named myStruct, we do
    Code:
    myStruct.memberOfStruct
    So, if myStruct is a pointer, you have to do this
    Code:
    (*myStruct).memberOfStruct
    C offers a shortcut for this, the arrow ->. So, the above code is equivalent to this code
    Code:
    myStruct->memberOfStruct
    So, example No.2 with pointers!
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define MAX 100
    
    struct student{
        char name[15];
        int age;
    };
    
    typedef struct student student;
    
    struct listStudent{
        student arrayStd[MAX];
        int size;
    };
    
    typedef struct listStudent listStudent;
    
    int main(void)
    {
        int i;
        /* Pointers. */
        listStudent* lStd = NULL;
        
        /* Allocate memory for where the pointer points to! */
        lStd = malloc(sizeof(listStudent));
        /* Check that malloc is successful, by checking if */
        /* pointer lStd is NULL                                     */
        
        /* Initialize */
        /* So, here we need to use the arrow, since */
        /* lStd is a pointer */
        lStd->size = 0;
        /* Create 2 students */
        /* 1st */
        /* lStd is a pointer, so we use the arrow */
        /* the array of students is of type student */
        /* ,thus we should use the dot in order to access */
        /* it's members */
        strcpy(lStd->arrayStd[0].name, "Samaras");
        lStd->arrayStd[0].age = 20;
        lStd->size++;
        
        /* 2nd */
        strcpy(lStd->arrayStd[1].name, "Midha");
        lStd->arrayStd[1].age = 21;
        lStd->size++;
        
        for(i = 0 ; i < lStd->size ; i++)
        {
            printf("Student %s is %d years old\n", lStd->arrayStd[i].name, lStd->arrayStd[i].age);
        }
        
        /* Don't forget to free!!! */
        free(lStd);
        
        return 0;
    }
    @the other guy (He doesn't have a name to reference him :/ ), no this is not the only difference!!!! Never forget to free the memory that you allocated with malloc
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 01-08-2013, 07:55 AM
  2. Typedef Structs inside Typdef structs
    By gremory in forum C Programming
    Replies: 21
    Last Post: 12-30-2011, 07:48 PM
  3. [ noob question ] Help with structs within structs
    By Riverfoot in forum C Programming
    Replies: 3
    Last Post: 04-26-2011, 07:24 PM
  4. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  5. passing structs & pointers to structs as arguments
    By Markallen85 in forum C Programming
    Replies: 6
    Last Post: 03-16-2004, 07:14 PM

Tags for this Thread