chinswain
asked on
Please check my Structure problem (simple question!)
Hi, im having a brain killer problem with my structure, bellow is the code, all is fine untill i add another field to the structure, char name[20] is what i want to add, so that each record has the persons name. the error on compile i get is:
error C2440: '=' : cannot convert from 'char [5]' to 'char [20]'
Please can you also detail why it does not work and what you changed?
Note the this is not the compete code, just the important bits.
typedef struct student STUDENT;
#define ARR_SIZE 05
struct student
{
long sid;
int mark;
char grade;
char name[20];
};
fillArray(staff,ARR_SIZE);
void fillArray(student s[], int n)
{
s[0].sid= 301111;
s[0].mark= 50;
s[0].grade= 'B';
s[0].name= "John, WS";
s[1].sid= 302222;
s[1].mark= 90;
s[1].grade= 'A';
s[1].name= "John, WS";
s[2].sid= 303333;
s[2].mark= 35;
s[2].grade= 'C';
s[2].name= "John, WS";
s[3].sid= 306985;
s[3].mark= 70;
s[3].grade= 'B';
s[3].name= "John, WS";
s[4].sid= 304578;
s[4].mark= 18;
s[4].grade= 'E';
s[4].name= "John, WS";
}
Thanks all.
error C2440: '=' : cannot convert from 'char [5]' to 'char [20]'
Please can you also detail why it does not work and what you changed?
Note the this is not the compete code, just the important bits.
typedef struct student STUDENT;
#define ARR_SIZE 05
struct student
{
long sid;
int mark;
char grade;
char name[20];
};
fillArray(staff,ARR_SIZE);
void fillArray(student s[], int n)
{
s[0].sid= 301111;
s[0].mark= 50;
s[0].grade= 'B';
s[0].name= "John, WS";
s[1].sid= 302222;
s[1].mark= 90;
s[1].grade= 'A';
s[1].name= "John, WS";
s[2].sid= 303333;
s[2].mark= 35;
s[2].grade= 'C';
s[2].name= "John, WS";
s[3].sid= 306985;
s[3].mark= 70;
s[3].grade= 'B';
s[3].name= "John, WS";
s[4].sid= 304578;
s[4].mark= 18;
s[4].grade= 'E';
s[4].name= "John, WS";
}
Thanks all.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Several other pretty critical items come popping out. Basically, this shouldn't compile at all.
>> typedef struct student STUDENT;
You are declaring a type STUDENT that is a 'struct student'. You don't use it here in your example.
>> #define ARR_SIZE 05
>> struct student
>> {
>> long sid;
>> int mark;
>> char grade;
>> char name[20];
>> };
>> fillArray(staff,ARR_SIZE);
>> void fillArray(student s[], int n)
'student' is not a known type. 'STUDENT' is, and 'struct student' is too.
'n' is not used.
>> {
>> s[0].sid= 301111;
>> s[0].mark= 50;
>> s[0].grade= 'B';
>> s[0].name= "John, WS";
>> /* Deleted for brevity */
>> }
As imladris said, C won't set "John, WS" into 's[0].name' with the code that you've got. You need to call strcpy() to COPY the string into the struct.
Kent