Solved

Please check my Structure problem (simple question!)

Posted on 2003-12-02
2
255 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

Independent Software Vendors: 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

Suggested Solutions

Title # Comments Views Activity
why debugging a macro i s difficult 10 43
How to get time_t value that is a week old 6 169
Handling string inputs in C/Linux 23 195
Reactjs with .NET 3 75
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

733 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