[英]How to use a struct inside another struct (while using malloc and 2D arrays?) in C
I am new to using struct, hope you can help me. 我是使用struct的新手,希望您能为我提供帮助。 I want to have a nested struct such that a member of A is type B struct.
我想要一个嵌套结构,以使A的成员是B型结构。 The code I have so far is shown below.
我到目前为止的代码如下所示。
typedef struct B
{
int b1;
} b;
typedef struct A
{
b a1;
}a;
But I also need 'a1' to be a dynamic 2d array, so I did the following code (not sure if this is right as i used a pointer to a pointer for this in order for the 'array' size to be set at runtime): 但是我还需要将'a1'设为动态2d数组,因此我执行了以下代码(不确定这样做是否正确,因为我为此使用了指向指针的指针,以便在运行时设置'array'的大小):
typedef struct B
{
int b1;
} b;
typedef struct A
{
b **a1;
}a;
To store memory I used malloc(didnt include the error detection part). 为了存储内存,我使用了malloc(didnt包括错误检测部分)。 I need to refer and compare each member of a1 by x, shown below (where a and b are integers).
我需要通过x来引用和比较a1的每个成员,如下所示(其中a和b是整数)。 Can I do this?:
我可以这样做吗?:
a* one;
b** two;
one= malloc(10* sizeof(a*));
two= malloc(10* sizeof(b*));
for(x = 1; x <= 10; x++)
{
two[x] = malloc(10* sizeof(b));
}
if(one[x].two[a][b] == x)
one[x].two[a][b].b1 =2;
Sorry if my code doesn't make sense at all. 抱歉,我的代码根本没有意义。
I also need 'a1' to be a 2d array, so I did the following code (not sure if this is right):
我还需要将“ a1”设为2d数组,所以我做了以下代码(不确定这是否正确):
typedef struct B { int b1; } b; typedef struct A { b **a1; }a;
The code provides valid C declarations, but there is no 2d array in sight. 该代码提供了有效的C声明,但是看不到2d数组。 Member
a1
of struct A
is a pointer to pointer to struct B
, which is not at all the same thing. struct A
成员a1
是指向struct B
指针,这根本不是同一回事。 If you truly want to declare a 2d array then you must give its bounds, for instance 如果您确实要声明2d数组,则必须给出其边界,例如
typedef struct A {
b a1[5][7];
} a;
If you don't know the needed sizes until runtime, however, then the double pointer is indeed what you want -- just do not confuse it with a 2D array. 但是,如果直到运行时才知道所需的大小,那么双指针确实是您想要的-只是不要将其与2D数组混淆。
To store memory I used malloc(didnt include the error detection part).
为了存储内存,我使用了malloc(didnt包括错误检测部分)。 I need to refer and compare each member of a1 by x, shown below (where a and b are integers).
我需要通过x来引用和比较a1的每个成员,如下所示(其中a和b是整数)。
Well no, a
and b
cannot be integers because you're already using them as type names in the same scope. 好吧,
a
和b
不能是整数,因为您已经在同一范围内将它们用作类型名称。
Can I do this?:
我可以这样做吗?:
a* one; b** two; one= malloc(10* sizeof(a*)); two= malloc(10* sizeof(b*)); for(x = 1; x <= 10; x++) { two[x] = malloc(10* sizeof(b)); }
In addition to the name collision already mentioned, your allocation of one
is wrong. 除了已经提到的名称冲突,您分配
one
是错误的。 It should be ... 它应该是 ...
one = malloc(10 * sizeof(a));
... or even better ... ...甚至更好...
one = malloc(10 * sizeof(*one));
. 。 Your allocations for
two
and two[x]
are ok, but they would be better if rewritten in the same manner as my latter example above. 您对
two
和two[x]
分配是可以的,但是如果以与上面我后面的示例相同的方式重写,它们会更好。
if(one[x].two[a][b] == x) one[x].two[a][b].b1 =2;
The type of one[x].two[i][j]
(for integer i
and j
) is b
, which is to say struct B
. one[x].two[i][j]
(对于整数i
和j
)的类型为b
,即struct B
The type of x
is int
. x
的类型为int
。 These are not comparable, so no, you cannot evaluate the expression in the if
condition. 这些是不可比较的,因此不能,您不能在
if
条件下评估表达式。 The assignment to one[x].two[a][b].b1
is ok, though. 不过,对
one[x].two[a][b].b1
是可以的。
Instead of messing around with 2D dynamic memory allocation, you can fake a 2D array with a 1D array. 您可以搞乱2D数组和1D数组,而不用搞乱2D动态内存分配。
b *a1;
a1 = malloc(sizeof(b) * width * height);
for(i = 0; i < height; i++)
for(j = 0; j < width; j++)
do_stuff(a1[i*width + j]; //simulates a1[i][j]
The only thing this requires is that you store your width and height values somewhere, eg 唯一需要做的就是将宽度和高度值存储在某个地方,例如
typedef struct A {
b *a1;
int height;
int width;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.