简体   繁体   中英

initialization error in Structures

 struct college      
{  
        char name[30];      
        char course[30];     
        int fee;     
    };     
    struct college c1={"purnia","m.com",5000};      
    struct college c2;

 int main()  
{  
         struct college c2={"magadh","hazipur",5200}; //1   
    c2.name="magadh"; // 2     
    c2.course="fine arts";  // 3  
    c2.fee=3000;    //4  
    strcpy(c2.name,"godda"); //5

}  

The line 2 and 3 gives error as incompatible type assignment whereas 1 and 5 works fine.This happens only with String type members.Is it because arrays can not be assigned but then why does line 1 works well. Thanks in advance.

Line 1 works because you're not initializing members with a pointer to a string, but you're initializing the array with that string content.
When you try to execute:

struct college c2={"magadh","hazipur",5200};

The compiler reserves in memory the space for the structure and inits the arrays name and course respectively with "magadh" and "hazipur" . But when you code:

c2.name="magadh";

You're trying to create an initialized string, "magadh" , in memory and then assign its address to the array c2.name .
While copying the new string to the array using strcpy() is perfectly legal.

The fields are arrays of char . You have to copy the char s into the array. Try something like strcpy() , as you did in line 5. You can't just assign a "string" like that.

Line 1 works because it's an initialization, not an assignment. The compiler gives it the value provided when it allocates space for it on the stack.

If the fields were of type char * (say you declared char *name ), you could assign it with a string literal. But the meaning is different: It would make name point to the string literal "magadh" . You could not modify the contents of this string since it was a string literal. For example, the following would result in undefined behaviour:

char *name = "magadh";
name[0] = 'n';  /* <-- undefined behaviour */

One reason this bad (other than C says it is) is that you don't know where the compiler puts the "magadh" in memory. It is allowed to put it in the code text section, which is not writable.

Notice the difference with the following:

char name[30] = "magadh";
name[0] = 'n';  /* <-- this is OK */

The name here resides on the stack as a local variable. It is an array of char , which is given its value (initialized) when the array is allocated. You can modify it because you are allowed to modify local variables, since they are on the stack.

In C, there really are no "strings". They are just contiguous char s somewhere that (hopefully) end in a null terminator '\\0' . If you want to "assign" one (as in your line 2 and 3), you have to explicitly say to copy char s from source to destination, beginning to end. This is what the strcpy() library function is for.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM