访问另一个结构数组中的结构成员

时间:2018-10-11 21:00:19

标签: c data-structures

-在这里学习C的学生-

已指示我创建一个[[StructureType],它是一个包含以下内容的原始(常规)数组的集合结构: [OtherStructureType]指针。“另外,” [StructureType]将填充10个动态分配的[OtherStructureType]结构。“

我很难在概念上和实际上理解这一点。这是我的目的。请在需要的地方纠正我...

1。)如果我们使用malloc()动态分配OtherStructureType的实例,它将是一个指针:

typedef struct{
    int age;
} OtherStructureType;

OtherStructureType * Person = malloc(1 * sizeof(OtherStructureType))

2。)如果StructureType的实例具有数组成员,那么我们可以将该指针放在该数组中:

typedef struct{
    int array[n];
} StructureType;

StructureType Collection;

Collection.array = Person;

3。)如果1和2是正确的,那么我们将如何确定Person的年龄?这样吗?

StructureType* ptr;
ptr = Collection;
ptr->array[i].age;

Edit1:我说Instance.array = Person;当我应该写Collection.array = Person; Edit2:Q2和Q3中的数组之间的清晰度

2 个答案:

答案 0 :(得分:0)

当然您的代码无效,甚至无法编译

#define NPEOPLE 10

typedef struct{
    int age;
} OtherStructureType;

typedef struct{
    OtherStructureType *ost;
} StructureType;


OtherStructureType * Person = malloc(1 * sizeof(*Person));
StructureType People;

People.ost = malloc(NPEOPLE * sizeof(People.ost));

Person -> age = 20;

People.ost[0] = Person;


/*Access*/

int age = People.ost[0] -> age; 

/* Or */

StructureType *People1 = malloc(sizeof(*People1));

People1 -> ost = malloc(NPEOPLE * sizeof(People1 ->ost));

Person -> age = 20;

People1 -> ost[0] = Person;

/*Access*/

int age = People1 -> ost[0] -> age; 

/* or */

typedef struct{
    OtherStructureType *ost[NPEOPLE];
} StructureType;

StructureType *People2 = malloc(sizeof(*People2));

Person -> age = 20;

People2 -> ost[0] = Person;

/*Access*/

int age = People1 -> ost[0] -> age; 

答案 1 :(得分:0)

任务

  
      
  • 创建一个“ [StructureType],它是一个包含[OtherStructureType]指针的原始(常规)数组的集合结构”。
  •   
  • 此外,“ [StructureType]将填充10个动态分配的[OtherStructureType]结构。”
  •   

部分答案(问题材料)

typedef struct
{
    int age;
} OtherStructureType;

OtherStructureType *person = malloc(1 * sizeof(OtherStructureType));

这大部分代码都很好(只要赋值位于函数内部)。出于风格原因,我将Person更改为person(如果不使用大写字母来区分类型和变量,则更容易区分它们。)

  
      
  1. 如果StructureType的实例具有数组成员,则...
  2.   

问题的这一部分基本上是完全错误的。您有一个整数数组,而不是指向OtherStructureType的指针数组。还不清楚n是什么定义的。要在结构定义中使用,它必须是一个常数,并且常数通常以所有大写字母(根据需要带有数字和下划线)编写。通常,这样的名称应该比N(也许是NUM_OTHERS)长,但在此示例中会这样做。另外,我通常在结构上使用标签,也使用typedef,但是我对结构没有标签。

enum { N = 10 };
typedef struct
{
    OtherStructureType *array[N];
} StructureType;

StructureType collection;

collection.array[0] = person;

只要赋值在函数内部,这将起作用。

  

3。那么我们如何确定人的年龄? …

StructureType *ptr = malloc(sizeof(*ptr));
OtherStructureType *other = malloc(sizeof(*other));
ptr->array[0] = other;
ptr->array[0]->age = 21;

这些大约是将起作用的最小更改(有关MCVE,请参见下面的nest31.c)。

更多更改

我认为您应该有类似的东西:

enum { N = 10 };
typedef struct StructureType
{
    int num_ptrs;
    OtherStructureType *array[N];
} StructureType;

num_ptrs元素使您可以跟踪数组元素中有多少个元素持有有效指针。访问无效的指针是疯狂或崩溃的秘诀,因此您需要知道有多少有效指针(假设如果num_ptrs-1,则元素0 .. num_ptrs > 0才有效)。如果您在不同的时间分配指向OtherStructureType的值,则适用。

有关MCVE,请参见下面的nest53.c

comment中,nielan问:

  

如果我们要有10个人,我们可以动态分配一个StructureType数组实例,而不是每个人单独做。

responded

是的,尽管您可能会做到:

typedef struct
{
    int num_ptrs;
    int num_used;
    OtherStructureType *people;
} StructureType;

然后您可以使用:

StructureType st;
st.num_ptrs = 10;
st.num_used = 0;
st.people = malloc(st.num_ptrs * sizeof(st.people[0]));

(记住检查分配是否成功)。或者:

StructureType *stp = malloc(sizeof(*stp));
stp->num_ptrs = 10;
stp->num_used = 0;
stp->people = malloc(stp->num_ptrs * sizeof(stp->people[0]));

在定义变量之后,只要您在函数内部,就可以初始化st而不是进行赋值。例如:

StructureType st = { .num_ptrs = 10, .num_used = 0,
                     .people = malloc(st.num_ptrs * sizeof(st.people[0]))
                   };

您仍然需要检查内存分配是否成功。现在,您可以使用以下符号:

st.people[0].age = 21;

代替使用:

st.people[0]->age = 21;

请注意,这两个表达式中的第一个表达式的指针访问量减少了,因此(边际!)访问效率更高。

有关MCVE,请参见下面的nest73.c


nest31.c

/* SO 5276-8854 */
#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    int age;
} OtherStructureType;

enum { N = 10 };
typedef struct
{
    OtherStructureType *array[N];
} StructureType;

int main(void)
{
    OtherStructureType *person = malloc(1 * sizeof(OtherStructureType));
    StructureType collection;

    collection.array[0] = person;
    collection.array[0]->age = 23;
    printf("Age: %d\n", collection.array[0]->age);

    StructureType *ptr = malloc(sizeof(*ptr));
    OtherStructureType *other = malloc(sizeof(*other));
    ptr->array[0] = other;
    ptr->array[0]->age = 21;
    printf("Age: %d\n", ptr->array[0]->age);

    free(ptr);
    free(other);
    free(person);
    return 0;
}

nest31的输出

Age: 23
Age: 21

nest53.c

/* SO 5276-8854 */

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

typedef struct
{
    int age;
} OtherStructureType;

enum { N = 10 };
typedef struct StructureType
{
    int num_ptrs;
    OtherStructureType *array[N];
} StructureType;

int main(void)
{
    OtherStructureType *person = malloc(1 * sizeof(OtherStructureType));
    StructureType collection = { 0, { 0 } };

    collection.array[0] = person;
    collection.num_ptrs = 1;
    collection.array[0]->age = 23;
    for (int i = 0; i < collection.num_ptrs; i++)
        printf("Age %d: %d\n", i, collection.array[i]->age);

    StructureType *ptr = malloc(sizeof(*ptr));
    OtherStructureType *other = malloc(sizeof(*other));
    ptr->num_ptrs = 1;
    ptr->array[0] = other;
    ptr->array[0]->age = 21;
    for (int i = 0; i < ptr->num_ptrs; i++)
        printf("Age %d: %d\n", i, ptr->array[i]->age);

    free(ptr);
    free(other);
    free(person);
    return 0;
}

nest53的输出

Age 0: 23
Age 0: 21

nest73.c

/* SO 5276-8854 */

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

typedef struct
{
    int age;
} OtherStructureType;

typedef struct
{
    int num_ptrs;
    int num_used;
    OtherStructureType *people;
} StructureType;

int main(void)
{

    StructureType st;
    st.num_ptrs = 10;
    st.people = malloc(st.num_ptrs * sizeof(st.people[0]));
    st.people[0].age = 21;
    st.people[1].age = 23;
    st.people[2].age = 19;
    st.num_used = 3;
    for (int i = 0; i < st.num_used; i++)
        printf("Age %d: %d\n", i, st.people[i].age);

    StructureType *stp = malloc(sizeof(*stp));
    stp->num_ptrs = 10;
    stp->people = malloc(stp->num_ptrs * sizeof(stp->people[0]));
    stp->people[0].age = 51;
    stp->people[1].age = 49;
    stp->people[2].age = 37;
    stp->num_used = 3;

    for (int i = 0; i < stp->num_used; i++)
        printf("Age %d: %d\n", i, stp->people[i].age);

    free(st.people);
    free(stp->people);
    free(stp);
    return 0;
}

nest73的输出

Age 0: 21
Age 1: 23
Age 2: 19
Age 0: 51
Age 1: 49
Age 2: 37
相关问题