Solved

Please check my Structure problem (simple question!)

Posted on 2003-12-02
2
253 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
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:Kdo
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How do I validate a simple fgets() statement in C programming? 15 139
XCode crashes while uploading to app store. 5 262
how to understand recursion 12 226
Detect CR LF to each line 12 160
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

770 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