2

结构如下,内部结构在外部结构内部。两个结构都是动态分配的。当我尝试通过引用来访问内部结构以更改地址值时,就会出现问题。

例如:

typedef struct{
    char address[32];
}inner;

typedef struct{
    struct inner innerStruct;
}outer;

main(){

    int i = 0;
    outer* outerArray;
    outer* outerReference;
    inner* innerReference;

    /* create 20 outer structs */
    outerArray = malloc(20 * sizeof(outerArray*));

    /* for each outer struct, dynamically allocate 10 inner structs */

    for(i = 0; i < 10; i++)
    {
        outerReference = outerArray + i;
        outerReference->innerStruct = malloc(10 * sizeof(outerReference->innerStruct);
    }

}

如何访问outerArray[3][innerStruct[4]第 4 个外部结构的第 5 个内部结构并更改其地址值?

4

2 回答 2

2

如果您想使用,请声明innerStructinner *,而不是 a 。struct innermalloc()

正如您所声明的,inner当您创建一个outer.

另请注意,由于您使用了 ,因此在声明该类型的变量时typedef不需要关键字。struct

这是编译和运行的代码的更正版本:

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

typedef struct {
  char address[32];  // 32 chars are allocated when an inner is created
} inner;

typedef struct {
  inner innerStruct;  // innerStruct is allocated when an outer is created
} outer;

typedef struct {
  inner *innerStruct;  // innerStruct must be allocated explicitly
} outer2;

int main(int argc, char *argv[]) {
  int i = 0;
  outer  *outerArray;
  outer2 *outer2Array;

  outer  *outerReference;
  outer2 *outer2Reference;

  /* create 20 outer structs (should check for out-of-mem error) */
  outerArray = malloc(20 * sizeof(outer));

  for (i = 0; i < 10; ++i) {
    outerReference = outerArray + i; // ptr to i'th outer
    // Note: innerStruct.address bcz it's a structure
    sprintf(outerReference->innerStruct.address, "outer struct %d", i);
  }

  /* create 20 outer2 structs */
  outer2Array = malloc(20 * sizeof(outer2));

  /* for each outer struct, dynamically allocate 10 inner structs */
  for (i = 0; i < 10; ++i) {
    outer2Reference = outer2Array + i;
    outer2Reference->innerStruct = malloc(sizeof(inner));
    // Note: innerStruct->address bcz it's a pointer
    sprintf(outer2Reference->innerStruct->address, "outer2 struct %d", i);
  }

  /* print all the data and free malloc'ed memory */
  for (i = 0; i < 10; ++i) {
    printf("outer: %-20s, outer2: %-20s\n",
      outerArray[i].innerStruct.address,
      outer2Array[i].innerStruct->address);
      free(outer2Array[i].innerStruct);
  }
  free(outer2Array);
  free(outerArray);
  return 0;
}
于 2012-04-29T21:36:24.253 回答
0

打印address第 4 个外部结构的第 5 个内部结构的成员,将数据复制到其中并再次打印:

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

typedef struct{
    char address[32];
}inner;

typedef struct{
    inner* innerStruct;
}outer;

int main()
{
    int i = 0;
    outer* outerArray;
    outer* outerReference;
    /* inner* innerReference; */ /* unused */

    /* create 20 outer structs */
    outerArray = malloc(20 * sizeof(*outerArray));

    /* for each outer struct, dynamically allocate 10 inner structs */

    for(i = 0; i < 10; i++)
    {
        outerReference = outerArray + i;
        outerReference->innerStruct = malloc(10 * sizeof(*outerReference->innerStruct));
    }

    printf("address: '%s'\n", outerArray[3].innerStruct[4].address);

    strcpy(outerArray[3].innerStruct[4].address, "<emtpy>");

    printf("address: '%s'\n", outerArray[3].innerStruct[4].address);

    return 0;
}

下次您发布代码时,请善待并使其编译。

于 2012-05-01T13:59:03.960 回答