Help with structures

This is a discussion on Help with structures within the C Programming forums, part of the General Programming Boards category; I am writing a program that will list 15 students by student number, student name, and their 5 test grades ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Help with structures

    I am writing a program that will list 15 students by student number, student name, and their 5 test grades in a table while using a stucture.

    examaple:
    1 John Doe 94 87 56 42 99
    2 Jane Doe 99 89 81 72 100

    I want to prepopulate the table with the student number and the student name, and print it out to make sure the table looks ok.

    The student name is a string of characters that will vary in length, and this is where I get stuck, complier is returning an "illegal zero-sized array.

    I can't figure how to get the variable length students name into the table, please see code snippet, and please help

    Code:
    struct sgrades
    {
    	int number;
    	char names[];
    	int test1;
    	int test2;
    	int test3;
    	int test4;
    	int test5;
    		
    } grade[16];
    
    struct sgrades grade[16] = {{0, 0}, {1, Jon Doe}, {2, Jane Doe}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {7, 1}, {8, 1}, {9, 1}, {10, 1}, {11, 1}, {12, 1}, {13, 1}, {14, 1}, {15, 1}};

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    You can not do that. Try

    Code:
    char *names;  /* or use the below assignment */
    char names[MAX_LEN] /*MAX_LEN can be defined prior to calling main(), ie #define MAX_LEN 25 */

  3. #3
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    slingerland3g points out you need to allocate memory. For example, I'm using the hard-coded 50 for this example.

    The first declaration of the structure is really just a template. So I would use typedef.
    Code:
    typedef struct
    {
    	int number;
    	char names[50];
    	int test1;
    	int test2;
    	int test3;
    	int test4;
    	int test5;
    		
    } sgrades;
    
    sgrades grade[16] = {{0, 0}, {1, "Jon Doe"}, {2, "Jane Doe"}, {3, 1}, {4, 1}, {5, 1}, {6, 1}, {7, 1}, {8, 1}, {9, 1}, {10, 1}, {11, 1}, {12, 1}, {13, 1}, {14, 1}, {15, 1}};
    Also, note I enclosed the strings with quotation marks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structures and dynamically allocated arrays
    By Bandizzle in forum C Programming
    Replies: 7
    Last Post: 10-04-2009, 02:05 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Structures within Structures
    By Misko82 in forum C Programming
    Replies: 2
    Last Post: 08-27-2007, 12:25 AM
  4. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 05:31 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 12:59 PM

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