?
Solved

Please check my Structure problem (simple question!)

Posted on 2003-12-02
2
Medium Priority
?
257 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 1200 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 46

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

762 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