Thread: Referencing a structure inside an array

  1. #1
    Registered User
    Join Date
    May 2010

    Referencing a structure inside an array


    I'm working on a program to sort students by last name, first name, middle initial, gpa, etc. I've defined a data structure called "Student."

    typedef struct
       char last[21];
       char first[11];
       char mi;
       double gpa;
    I wrote a function which will scan a line of a text file, then fill in each part of the "Student" data structure, and return a variable (type Student), called studentx.

    Now, I need to declare an array of type "Student" and input "studentx" into each index, after it comes out of the function. Keep in mind every time I call the function, a different "studentx" will be returned.

    My first problem is that I don't think I'm defining it right. I have this

    struct Student studentList[1000];
    but I get the error "studentrpt.c:32: error: array type has incomplete element type"

    any ideas?

    to input my "student" variable into the studentList array, here's what I have. (it says "i" because it's in a loop, but i'm not worried about the loop right now, so don't worry about it either. As stated, the "getInfo" function returns type Student.

    studentList[i] = getInfo(input, student);
    Now say I want to print a specific thing from a specific index of my studentList array, for example, I want to print the first name of the student in the 27th index of the array. How do I do that? What variable do i put in the printf statement?

    is it this?


    Any help is appreciated, thanks!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    The only difference between accessing an array element and a single variable instance is the addition of [ here ].
    struct foo onething, arrayofthing[ 5 ];
    ... = baz;
    arrayofthing[ here ].bar = baz;
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2009
    You don't need the 'struct' part of your array declaration since you used typedef in the struct.

    Student studentList[1000];
    This should do it.

    is it this?

    Yes, that's it.

    You mention that your getInfo function will return a new instance of Student in each call, which means that you are allocating memory inside the function for each struct. But, you have already set aside memory for 1000 Student structs, you'll need to pass a pointer to Student to the array, and make it an array of Student pointers.

    Or, do as you do now, but pass the entire studentList as argument to your getInfo function, and fill it in, in place directly from the file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array and structure problem
    By bluetxxth in forum C Programming
    Replies: 3
    Last Post: 04-13-2010, 06:56 AM
  2. Structure array problem
    By Birdhaus in forum C++ Programming
    Replies: 2
    Last Post: 11-21-2005, 09:59 PM
  3. Multidimensional Array in a Structure
    By Night_Blade in forum C Programming
    Replies: 3
    Last Post: 04-04-2005, 08:14 PM
  4. Filling an array in a structure
    By thephreak6 in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 06:05 PM
  5. referencing C structure elements in assembly
    By BigAl in forum C Programming
    Replies: 7
    Last Post: 09-10-2001, 03:19 PM

Tags for this Thread