C - initialize an array of structures

I had a problem initializing an array of structures. I am not sure that I am doing this correctly because I get "initialization from an incompatible type of pointer" and "assignment from an incompatible type of pointer". I added to the code where I get these warnings, and when I try to print data from the structure, I just get garbage like @@ ###

typedef struct { char* firstName; char* lastName; int day; int month; int year; }student; 

// initialize the array

  student** students = malloc(sizeof(student)); int x; for(x = 0; x < numStudents; x++) { //here I get: "assignment from incompatible pointer type" students[x] = (struct student*)malloc(sizeof(student)); } int arrayIndex = 0; 

// add struct

  //create student struct //here I get: "initialization from incompatible pointer type" student* newStudent = {"john", "smith", 1, 12, 1983}; //add it to the array students[arrayIndex] = newStudent; arrayIndex++; 
+15
c pointers struct malloc
Nov 13 '10 at 16:35
source share
4 answers
 student** students = malloc(sizeof(student)); 

No no no!

You do not need ** . Do you want * and enough space for how many students you need

 student* students = malloc(numStudents * sizeof *students); for (x = 0; x < numStudents; x++) { students[x].firstName = "John"; /* or malloc and strcpy */ students[x].lastName = "Smith"; /* or malloc and strcpy */ students[x].day = 1; students[x].month = 12; students[x].year = 1983; } 
+25
Nov 13 2018-10-11
source share

Not relevant to compiler warnings, but your original malloc is wrong; Do you want to:

 malloc(sizeof(student *)* numStudents) 

To make room for numStudents pointers for the student. Line:

 students[x] = (struct student*)malloc(sizeof(student)); 

Must be:

 students[x] = (student*)malloc(sizeof(student)); 

There is no such thing as a "struct student". You have declared an unnamed structure and typedef'd it is a "student". Compare and compare with:

 struct student { char* firstName; char* lastName; int day; int month; int year; }; 

To create a type of "struct student", but requires you (in C) to explicitly refer to the struct student, and not just to the student elsewhere. This rule has been changed for C ++, so your compiler may be a bit fuzzy.

Concerning:

 student* newStudent = {"john", "smith", 1, 12, 1983}; 

It should be:

 student newStudent = {"john", "smith", 1, 12, 1983}; 

Since the curly brace syntax is a direct literal, and not something else you need to specify.

EDIT: upon reflection, I think aaa may have taken more review of this than me. Is it possible that you inadvertently use an extra level of pointer dereferencing everywhere? Therefore you need to:

 student* students = malloc(sizeof(student) * numStudents); /* no need for this stuff: */ /*int x; for(x = 0; x < numStudents; x++) { //here I get: "assignment from incompatible pointer type" students[x] = (struct student*)malloc(sizeof(student)); }*/ int arrayIndex = 0; 

and

 student newStudent = {"john", "smith", 1, 12, 1983}; //add it to the array students[arrayIndex] = newStudent; arrayIndex++; 

Specify that the array is not used outside the scope of newStudent. Otherwise, copying pointers to strings incorrectly.

+7
Nov 13 2018-10-11
source share
 student* students = malloc(sizeof(student)*numStudents); int x; for(x = 0; x < numStudents; x++) { student newStudent = {"john", "smith", 1, 12, 1983}; // string copy are wrong still students[x] = newStudent; } 
+2
Nov 13 2018-10-11
source share

Upon initialization, is this not so?

 student** students = (struct student**)malloc(sizeof(student*)*numStudents); 

However, why a pointer to a pointer? I think that with a pointer to a structure is enough.

-one
Nov 13 2018-10-11
source share



All Articles