Solved

Please check my Structure problem (simple question!)

Posted on 2003-12-02
2
256 Views
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];
};

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.
0
Comment
Question by:chinswain
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 16

Accepted Solution

by:
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.
0
 
LVL 45

Expert Comment

by:Kent Olsen
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.


Kent
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

688 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