I get “Use of undeclared identifier ‘data'” when I compile that code. As you can see the problem is that add_student function can’t “see” student array.
What to do so to work properly ?
#include <stdio.h>
typedef struct {
char *name;
int age;
char *sex;
int class;
}student;
void add_student(int, char*, int, char*, int);
int main (int argc, const char * argv[])
{
student data[5];
add_student(5, "Mery", 3, "female", 8);
return 0;
}
void add_student(int sequence, char *name, int age, char *sex, int class) {
strcpy(data[sequence].name, name);
data[sequence].age[13];
strcpy(data[sequence].sex, sex);
data[sequence].class[2];
}
The cleanest way to fix this is by passing
dataas an additional argument toadd_student().There are other errors, such as
5as the value ofsequenceand then using it to index intodata;strcpy()is incorrect as you haven’t allocated memory for thenameandsexfields;data[sequence].age[13]anddata[sequence].class[2]are not valid C.