C语言 为结构变量赋值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32698293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 12:09:03  来源:igfitidea点击:

Assign values to structure variables

c

提问by Frankie

A structure type is defined as:

结构类型定义为:

typedef struct student{
    int id;
    char* name;
    double score;
} Student;

I construct a variable of type Student and I want to assign values to it. How can I do that efficiently?

我构造了一个 Student 类型的变量,我想给它赋值。我怎样才能有效地做到这一点?

int main(){
    Student s1;

    int id = 3;

    char* name = getName(id);

    double score = getScore(id);

    /*Error
    s1 = {id, name, score};
    */

    /*  Can I avoid assigning values individually?
    s1->id = id;
    s1->name = name;
    s1->score= score;
    */

    return 0;
}

回答by Alexander Ushakov

In C99 standard you can assign values using compound literals:

在 C99 标准中,您可以使用复合文字赋值:

Student s1;
s1 = (Student){.id = id, .name = name, .score = score};

回答by Serge Ballesta

Beware, struct and pointer to struct are 2 different things.

注意,struct 和指向 struct 的指针是两种不同的东西。

C offers you:

C 为您提供:

  • struct initialization (only at declaration time):

    struct Student s1 = {1, "foo", 2.0 }, s2;
    
  • struct copy:

    struct Student s1 = {1, "foo", 2.0 }, s2;
    s2 = s1;
    
  • direct element access:

    struct Student s1 ;
    s1.id = 3;
    s1.name = "bar";
    s1.score = 3.0;
    
  • manipulation through pointer:

    struct Student s1 = {1, "foo", 2.0 }, s2, *ps3;
    ps3 = &s2;
    ps3->id = 3;
    ps3->name = "bar";
    ps3->score = 3.0;
    
  • initialization function:

    void initStudent(struct Student *st, int id, char *name, double score) {
        st->id = id;
        st->name = name;
        st->score = score;
    }
    ...
    int main() {
        ...
        struct Student s1;
        iniStudent(&s1, 1, "foo", 2.0);
        ...
    }
    
  • 结构体初始化(仅在声明时):

    struct Student s1 = {1, "foo", 2.0 }, s2;
    
  • 结构体复制:

    struct Student s1 = {1, "foo", 2.0 }, s2;
    s2 = s1;
    
  • 直接元素访问:

    struct Student s1 ;
    s1.id = 3;
    s1.name = "bar";
    s1.score = 3.0;
    
  • 通过指针操作:

    struct Student s1 = {1, "foo", 2.0 }, s2, *ps3;
    ps3 = &s2;
    ps3->id = 3;
    ps3->name = "bar";
    ps3->score = 3.0;
    
  • 初始化函数:

    void initStudent(struct Student *st, int id, char *name, double score) {
        st->id = id;
        st->name = name;
        st->score = score;
    }
    ...
    int main() {
        ...
        struct Student s1;
        iniStudent(&s1, 1, "foo", 2.0);
        ...
    }
    

Pick among those (or other respecting C standard), but s1 = {id, name, score};is nothing but a syntax error ;-)

从那些(或其他尊重 C 标准)中挑选,但s1 = {id, name, score};只是一个语法错误;-)

回答by Caleb

Can I avoid assigning values individually?

我可以避免单独分配值吗?

You can if the values are already part of a similar struct, that is, you can do this:

如果值已经是 similar 的一部分struct,您可以这样做,也就是说,您可以这样做:

Student s1 = {.id = id, .name = name, .score = score};

which creates an instance of Studentand initializes the fields you specify. This probably isn't really any more efficient than assigning values individually, but it does keep the code concise. And once you have an existing Studentinstance, you can copy it with simple assignment:

它创建一个实例Student并初始化您指定的字段。这可能并不比单独分配值更有效,但它确实使代码保持简洁。一旦你有一个现有的Student实例,你可以通过简单的赋值来复制它:

Student s2;
s2 = s1;    // copies the contents of s1 into s2

If the values are all in separate variables and you're not initializing the Student, then you'll probably need to assign the values individually. You can always write a function that does that for you, though, so that you have something like:

如果这些值都在单独的变量中并且您没有初始化Student,那么您可能需要单独分配这些值。但是,您始终可以编写一个为您执行此操作的函数,以便您拥有以下内容:

setupStudent(s3, id, name, score);

That'll keep your code short, ensure that the struct is populated the same way every time, and simplify your life when (not if) the definition of Studentchanges.

这将使您的代码保持简短,确保每次都以相同的方式填充结构,并在(不是如果)Student更改定义时简化您的生活。