简体   繁体   English

将结构数组分配给typedef结构

[英]Assigning Array of Structs to a typedef struct

How do I assign to a typedef struct an array of another struct with a similar structure. 如何将具有相似结构的另一个struct的数组分配给typedef struct结构。

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int age;
    int height;
} Person[3];

struct internalStruct {
    int age;
    int height;
};

int main(void) {

    //Possible
    Person bob = {{7,5},{4,2},{4,3}};

    //Is it possible to assign array to struct?
    struct internalStruct intr[3] = {{4,32},{2,4},{2,4}};
    Person job = intr; // Does not work :(. 
    printf("%d", jon[0].height);
    return 0;
}

You cannot assign to an array in C. You can initialize an array when you declare it, but an array expression cannot appear on the left side of an assignment operator. 您不能在C中为数组赋值。可以在声明数组时对其进行初始化,但是数组表达式不能出现在赋值运算符的左侧。

If you want to copy the value of an array object into another array object, you can use an explicit loop to assign each element (assuming the element type is assignable), or you can use memcpy() . 如果要将一个数组对象的值复制到另一个数组对象中,则可以使用显式循环来分配每个元素(假设元素类型是可分配的),也可以使用memcpy() (Note that a call to memcpy() needs to specify the number of bytes to copy; use sizeof for this.) (请注意,对memcpy()的调用需要指定要复制的字节数;为此请使用sizeof 。)

And your typedef Person : 还有你的typedef Person

typedef struct {
    int age;
    int height;
} Person[3];

is ill-advised. 是不明智的。 A Person object (variable) isn't a person; 一个Person对象(变量)不是一个人; it's an array of 3 persons (people?). 它是由3个人组成的数组(人?)。

My advice: Drop the typedef and just use a struct tag (as you already do for struct internalStruct ) and don't try to create a special name for the array type: 我的建议:删除typedef并仅使用struct标记(就像您对struct internalStruct所做的那样),不要尝试为数组类型创建特殊名称:

struct Person {
    int age;
    int height;
};

... ...

struct Person bob[] = {{7,5},{4,2},{4,3}};

(This is still confusing, since bob is three people.) (这仍然令人困惑,因为bob是三个人。)

And struct Person (as I've defined it here) and struct internalStruct are two distinct types. struct Person (如我在这里定义的)和struct internalStruct是两种不同的类型。 If you're trying to assign between these two types, it probably indicates a design flaw in your code; 如果您试图在这两种类型之间进行分配,则可能表明代码中存在设计缺陷。 objects you're assigning to each other should be of the same type. 您要彼此分配的对象应该是同一类型。

Recommended reading: the comp.lang.c FAQ , especially section 6 (arrays and pointers). 推荐阅读: comp.lang.c FAQ ,特别是第6节(数组和指针)。

I would not suggest this tough, since you might run into memory leaks when the two structs are different: 我不建议这样做,因为当两个结构不同时,您可能会遇到内存泄漏:

#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int age;
    int height;
} Person[3];

struct internalStruct {
    int age;
    int height;
};

int main(void) {

    //Possible
    Person bob = {{7,5},{4,2},{4,3}};

    //Is it possible to assign array to struct?
    struct internalStruct intr[3] = {{4,32},{2,4},{2,4}};
    Person* jon= (Person *)intr; // Does not work :(.
    printf("%d", jon[0]->height);
    return 0;
}

Your Person type is an array of three structures, each of which is similar to your struct internalStruct . 您的Person类型是由三个结构组成的数组,每个结构都与您的struct internalStruct相似。 So, you can't just assign a struct internalStruct to a Person , though you can (with some help) assign it to one of the elements of a Person . 因此,尽管可以(在一些帮助下)将struct internalStruct分配给Person ,但不能仅将其分配给Person

Also, copying arrays in C requires copying element by element, or copying the block of memory with a function like memcpy() . 同样,在C语言中复制数组需要逐个元素复制,或者使用memcpy()类的函数复制内存块。

So the simplest way to do this would be to define struct internalStruct before Person , and define Person in terms of struct internalStruct : 因此,最简单的方法是 Person 之前定义struct internalStruct ,并根据struct internalStruct定义Person

struct internalStruct {
  int age;
  int height;
};

typedef struct internalStruct Person[3];

Doing it this way makes it possible to assign a struct internalStruct to an element of Person without type mismatches. 这样,就可struct internalStruct Person一个元素分配一个struct internalStruct ,而不会出现类型不匹配的情况。 For example: 例如:

struct internalStruct s1 = {4,32},
                      s2 = {2,4},
                      s3 = {2,4};
Person jon;
jon[0] = s1;
jon[1] = s2;
jon[2] = s3;

If you have an array of three struct internalStruct s, you could copy it with a loop: 如果您有一个由三个struct internalStruct的数组,则可以使用循环将其复制:

struct internalStruct st[3] = { {4,32}, {2,4}, {2,4} };
Person jon;
for (int i = 0; i < 3; i++)
  jon[i] = st[i];

If you don't want to define Person in terms of struct internalStruct , then you have to make some assumptions, like that the layout of the two structures will be identical. 如果您不想根据struct internalStruct来定义Person ,那么您必须做一些假设,例如两个结构的布局是相同的。 If so, you could copy with memcpy() : 如果是这样,您可以使用memcpy()复制:

struct internalStruct intr[3] = { {4,32}, {2,4}, {2,4} };
Person jon;
memcpy(jon, intr, sizeof(Person));

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

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