简体   繁体   中英

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.

#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.

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() . (Note that a call to memcpy() needs to specify the number of bytes to copy; use sizeof for this.)

And your typedef Person :

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

is ill-advised. A Person object (variable) isn't a person; it's an array of 3 persons (people?).

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:

struct Person {
    int age;
    int height;
};

...

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

(This is still confusing, since bob is three people.)

And struct Person (as I've defined it here) and struct internalStruct are two distinct types. 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).

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 . 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 .

Also, copying arrays in C requires copying element by element, or copying the block of memory with a function like memcpy() .

So the simplest way to do this would be to define struct internalStruct before Person , and define Person in terms of struct internalStruct :

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. 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 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. If so, you could copy with memcpy() :

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

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