0

first post here. So I'm building a program which holds records on students in a struct. I want to use an array to have six elements, and then have each element as a struct.... I've got

typedef struct {
    char* name;
    char* surname;
    char* UUN;
    char* department;
    char gender;
    int age;
} student;

...

int main(void) {

int i;

student studentarr[5];

... So the task is to define the first three elements and have the user input the second three. I've tried defining by;

studentarr[0] = { "John", "Bishop", "s1234", "Inf", 'm', 18 };
studentarr[1] = { "Lady", "Cook", "s345", "Eng", 'f', 21 };
studentarr[2] = { "James", "Jackson", "s3456", "Eng", 'm', 17 };

But the compiler is giving me the error

studentDB2.c:27:18: error: expected expression
    studentarr[0] = { "John", "Bishop", "s1234", "Inf", 'm', 18 };
                    ^

studentDB2.c:28:18: error: expected expression
    studentarr[1] = { "Lady", "Cook", "s345", "Eng", 'f', 21 };
                    ^

studentDB2.c:29:18: error: expected expression
    studentarr[2] = { "James", "Jackson", "s3456", "Eng", 'm', 17 };
                    ^

How do I resolve this?

Another question is, does the declaration student studentarr[5] actually create six structs like;

studentarr[0]
studentarr[1]
studentarr[2]
studentarr[3]
studentarr[4]
studentarr[5]

I'm very aware at how basic this seems, but I have to sit an exam with this style of questions. All help is appreciated!

EDIT:

So I have a function

void printStudent(student s) {
    printf("Name: %s %s\n", s.name, s.surname);
    printf("UUN: %s\n", s.UUN);
    printf("Department: %s\n", s.department);
    printf("Gender: %c\n", s.gender);
    printf("Age: %d\n", s.age);
}

The compiler asks the user to enter the information in the student struct (at beginning of post) but now when the function printStudent runs, it's displaying the entered information like this

Student 5
Name: Nina Storrie
UUN: S3736PSYf
Department: PSYf
Gender: f
Age: 19

Why is UUN and Department printing like that?

  • regarding this: 'student studentarr[5]; ... So the task is to define the first three elements and have the user input the second three. I've tried defining by;' That is a total of 6 elements, so the declaration needs to be: 'student studentarr[6];' – user3629249 Aug 14 '15 at 20:43

2 Answers2

2

If you want to have an array to store information for 6 students then you have to declare it like

student studentarr[6];
                  ^^^

You could initialize it when it is declared

student studentarr[6] =
{
    { "John", "Bishop", "s1234", "Inf", 'm', 18 },
    { "Lady", "Cook", "s345", "Eng", 'f', 21 },
    { "James", "Jackson", "s3456", "Eng", 'm', 17 }
};

In this case only three elements of the array explicitly initialized. All other elements implicitly initialized by zeroes.

If you want to assign values to the elements of the array you can do it using compound literals.

For example

student studentarr[6];
studentarr[0] = ( student ) { "John", "Bishop", "s1234", "Inf", 'm', 18 };

Otherwise you have to assign each data member of an element of the array separatly.

For example

studentarr[0].name = "John";
studentarr[0].surname = "Bishop";

and so on

EDIT: After you updated you code then this this initialization data member department declared like

char department[3];

with string literal "Inf" is not valid if you want to store string. The string literal has 4 characters including the terminating zero. So if you want that data member department also would contain such strings you have to declare it like

char department[4];
                ^^
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

You can't use syntax like this to set the values of a struct:

studentarr[0] = { "John", "Bishop", "s1234", "Inf", 'm', 18 };

This syntax is only valid at the time the variable is defined:

student studentarr[5] = { 
    { "John", "Bishop", "s1234", "Inf", 'm', 18 }, 
    { "Lady", "Cook", "s345", "Eng", 'f', 21 }, 
    { "James", "Jackson", "s3456", "Eng", 'm', 17 },
    ... 
};

Also, a definition like studentarr[5] creates an array with five elements with indexes 0 to 4.

One other issue with your struct. Because you define the fields to be used as strings as char *, the values you give them at initialization time are read-only. You should instead define these fields as character arrays.

For example:

typedef struct {
    char name[50];
    char surname[50];
    char UUN[20];
    char department[10];
    char gender;
    int age;
} student;

Edit:

A C string includes a NUL terminator character, so make sure you leave enough room for that character. Also, use a length specifier in scanf to make sure the user doesn't enter too many characters for the field.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • Or change them to `const char * ..` – Kninnug Aug 14 '15 at 20:34
  • Thanks, it compiled and ran, but this happened Enter student 4 details: Forename: Ste Segmentation fault: 11 Any suggestions – Stephen McGarry Aug 14 '15 at 20:37
  • @Kninnug, changing the field definitions to 'const char *' will not change the fact that any effort to modify the strings (not the pointers) will result in a seg fault. and the 'const' modifier will be a major problem when trying to add the last 2 students – user3629249 Aug 14 '15 at 20:37
  • @user3629249 no, but it'll let the compiler catch attempts to change the strings through the `struct`s. – Kninnug Aug 14 '15 at 20:39
  • @user3629249 You'll need to show your full code for us to see what's happening. – dbush Aug 14 '15 at 20:42