简体   繁体   English

结构变量初始化

[英]struct variable initialization

I tried to initialize struct variable as following: 我试图初始化结构变量,如下所示:

struct Abc{
    char str[10];
};

int main(){
    struct Abc s1;
    s1.str="Hello";  //error
}

I can understand this behavior because it is same as 我可以理解此行为,因为它与

char str[10];
str="Hello"; // incompatible types

But look at following initializations 但是请看下面的初始化

struct Abc s1={"Hello"};   //This is fine

struct Abc s2={.str="Hello"};  //This is also fine

I remember in my graduation, I read lot of text books which said these both initializations are one and same thing (ie initialing struct variables using { } notation and explicitly using (.) operator are same thing ). 我记得我毕业时读过很多教科书,它们说这两个初始化是一回事(即使用{}表示法初始化结构变量和显式使用(。)运算符是同一件事)。 But above discussion proves that they are not same. 但是以上讨论证明它们并不相同。

My question is what exactly is difference between these initializations? 我的问题是这些初始化之间到底有什么区别?

The difference is, these two lines 区别在于,这两行

struct Abc s1={"Hello"};   //This is fine
struct Abc s2={.str="Hello"};  //This is also fine

are initialization , while this 初始化 ,而这

s1.str="Hello";

is assignment . 任务 You can initialize a char array to a string literal, but not through assignment. 您可以将char数组初始化为字符串文字,但不能通过分配。

This struct Abc s2={.str="Hello"}; struct Abc s2={.str="Hello"}; can be called as designated initialization, whereas struct Abc s1={"Hello"}; 可以称为指定的初始化,而struct Abc s1={"Hello"}; general initialization. 一般初始化。

Let me explain the advantage of this designated initialization with example. 让我用示例解释这种指定的初始化的优点。

Assume structure is having variable like struct ex{ char *name; int age; char *city; char *country } 假设结构具有变量,如struct ex{ char *name; int age; char *city; char *country } struct ex{ char *name; int age; char *city; char *country } struct ex{ char *name; int age; char *city; char *country } . struct ex{ char *name; int age; char *city; char *country } In this if you want initialize only city&country designated initialization can be used. 在这种情况下,如果要初始化,则只能使用城市和国家/地区指定的初始化。 But in case of general initialization each members needs to be initialized separately. 但是在一般初始化的情况下,每个成员都需要分别初始化。 This this overhead for the programmer&complex also. 这对于程序员和复合体来说也是开销。

The following assignment statements are exactly same (but wrong): 以下赋值语句完全相同(但错误):

s1.str="Hello"; & str = "Hello"; str = "Hello"; .

The difference is just that first one is a string inside a struct . 不同之处在于,第一个是struct内部的字符串。

And by the way, initialization means assigning value to a variable at the time of its definition . 顺便说一句, 初始化意味着在定义变量时将值赋给变量。

struct Abc s1; declares and defines s1 so you initialize it here as: 声明并定义s1因此您可以在此处将其初始化为:

struct Abc s1={"Hello"};   //This is fine
struct Abc s2={.str="Hello"};  //This is also fine 

Doing this 这样做

struct Abc s1;
s1.str="Hello";

is not a initialization , it is just assigning constant string literal to str pointer which is incompatible. 不是初始化 ,它只是常量字符串常量分配给不兼容的str指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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