Arrays of pointers inside structures.

This is a discussion on Arrays of pointers inside structures. within the C Programming forums, part of the General Programming Boards category; Hello. I am writing a student records program for class. We are supposed to make two structures, one for students ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    15

    Arrays of pointers inside structures.

    Hello.

    I am writing a student records program for class. We are supposed to make two structures, one for students and one for courses. Inside each structure the needs to be an array of pointers to the other structure. I have the structures set up so:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define MAX_STUDENT_NAME 20  //maximum characters for first or last name
    #define MAX_CLASS_NAME 30     //maximum characters for class name
    
    typedef	struct student{                         //student info
    	int ID;
    	char fname[MAX_STUDENT_NAME];
    	char lname[MAX_STUDENT_NAME];
    	struct course *myClasses[3];
    	int numClasses;
    	int grades;
    
    }student_t;
    
    
    typedef struct course{   // class info
    	char Title[MAX_CLASS_NAME];
    	char courseID [5];
    	 student_t *myStudents[10];
    	int grades;
    	int credits;
    }course_t;
    The trouble I am having is inputting the information. I have been trying something like
    Code:
    strcpy(course_t.myStudents[0].fname, "Bob");
    but I get different errors with each variation. Any help would be appreciated.

    but I am getting

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    What might these different errors be?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    15
    For that particular one

    error C2065: 'myStudents' : undeclared identifier
    error C2228: left of '.fname' must have class/struct/union

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    course_t is an alias for the struct course type. Therefore, course_t.myStudents[0] does not make sense. You would need a course_t object (let's name it course) to be able to use course.myStudents[0]. But if you did, then course.myStudents[0].fname would not make sense, since course.myStudents[0] would be a pointer. Instead, you would use: course.myStudents[0]->fname.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Break it down:
    Code:
    // declare a variable of type course_t
    course_t myCourse;
    
    // allocate enough space for the first student
    myCourse.myStudents[0] = malloc(sizeof(student_t));
    
    // give the first student a name
    strcpy(myCourse.myStudents[0]->fname, "Bob");
    Is there any reason why you want an array of 10 student_t pointers instead of an array of 10 student_t's?

    [edit] Its always good to hit submit and then see that someone already posted before you [/edit]

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    35
    one thing that looks troublesome is this

    Code:
    student_t *myStudents[10];
    to me, you have declared a 2d array. but then you write this

    Code:
    strcpy(course_t.myStudents[0].fname, "Bob");
    which is referring to a whole row of structs. I think you want to refer to a single struct only, so something like

    Code:
    strcpy(course_t.myStudents[0][0].fname, "Bob");
    would be more appropriate

    but thats not the biggest problem. there is something very wrong with this line still. ill give you an equivalent line of code using integers only;

    Code:
     int = 3*5;
    int is a data type, you cant assign it a value. similarly, course_t is a data type, not a variable, so you cannot alter it.

    so how would you solve this problem using integers? you would write

    Code:
     int variableNameHere;
    variableNameHere = 3*5;
    similarly, you need to write

    Code:
    course_t variableNameHere;
    strcpy(variableNameHere.myStudents[0][0].fname, "Bob");
    those are a few of the bigger problems.

    i also dont like the fact that your structs include pointers to eachother. to me that means they would be infinitely big. like you have structA, and inside structA is structB, and inside structB is structA, and inside structA is structB, etc.

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    15
    Thanks guys, that got it.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,973
    Quote Originally Posted by Brain_Child
    to me, you have declared a 2d array.
    No, it is a declaration of an array of 10 pointers to student_t objects. These pointers could indeed point to the first element of arrays of student_t objects, but they could also point to lone student_t objects.

    Quote Originally Posted by Brain_Child
    i also dont like the fact that your structs include pointers to eachother. to me that means they would be infinitely big. like you have structA, and inside structA is structB, and inside structB is structA, and inside structA is structB, etc.
    That would be impossible (and in fact type T1 cannot have objects of type T2 as members if objects of type T2 have objects of type T1 as members; pointers would then be used to work around this). Rather, this particular usage seems fine since it is just a way to access the courses that a student is enrolled in, given the student.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers or Arrays?
    By magda3227 in forum C Programming
    Replies: 3
    Last Post: 07-09-2008, 09:52 AM
  2. use of pointers with char arrays
    By txp200 in forum C Programming
    Replies: 6
    Last Post: 05-12-2004, 06:52 PM
  3. Help understanding arrays and pointers
    By James00 in forum C Programming
    Replies: 2
    Last Post: 05-27-2003, 01:41 AM
  4. Array pointers to Multi-Dimensional Arrays
    By MethodMan in forum C Programming
    Replies: 3
    Last Post: 03-18-2003, 08:53 PM
  5. Pointers and Arrays
    By C Seņor in forum C Programming
    Replies: 4
    Last Post: 06-16-2002, 01:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21