Please check my Structure problem (simple question!)

Posted on 2003-12-02
Last Modified: 2010-04-15
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];


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.
Question by:chinswain
LVL 16

Accepted Solution

imladris earned 300 total points
ID: 9862709
Well, I can't be sure whether this is your error or not, but it is very bad to make these assignments:

s[4].name="John, WS";

the name variable in the student structure is an array. So name points to the start of the memory reserved for the array. The above assignment would reassign name to point to some place in the constant pool. To assign the name to the name array you would need to use string copy:

strcpy(s[4].name,"John, WS");

This will copy the characters of the name to the name array.

If that doesn't put you on the right path, it might be helpful if you pointed out for which line the compiler was emitting the error message.
LVL 45

Expert Comment

ID: 9863569

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.


Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now