[英]Initializing a struct with another struct
How do I assign a value from struct to another. 如何将struct中的值分配给另一个。 Here is my code.
这是我的代码。
I believe I am assigning the address of the struct which is what I don't want to do. 我相信我正在分配该结构的地址,这是我不想做的。 I want to assign the values of "temp" to 'a'.
我想将“ temp”的值分配给“ a”。 I commented the section I need help on.
我评论了需要帮助的部分。 Thanks
谢谢
Also off-topic.. How do I post code without having to indent myself every line, line-by-line? 也是题外话。如何发布代码而不必每行逐行缩进?
#include <stdio.h>
#include <stdlib.h>
#define SIZE 10
typedef struct dynArrStruct
{
double value1;
int value2;
int value3;
}dynArr;
void init(dynArr* a)
{
dynArr temp;
temp.value1 = (double)(rand()) * rand() / rand();
temp.value2 = rand()%100;
temp.value3 = rand()%1000;
printf("In init(): value1: %14.5f, value2: %6d, value3: %6d\n",
temp.value1, temp.value2, temp.value3);
a = &temp; // THIS LINE
}
int main(int argc, char** argv)
{
int i;
dynArr a1[SIZE];
dynArr* a2[SIZE];
for(i = 0; i < SIZE; i++)
{
init(&(a1[i]));
init(a2[i]);
}
return 0;
}
Use assignment (assuming a
is pointing to valid memory): 使用分配(假设
a
指向有效内存):
*a = temp;
Note this works because the members of the struct
are not pointers. 请注意,这是可行的,因为该
struct
的成员不是指针。 Using this technique (or memcpy()
) on a struct
that contained member pointers would result in both struct
s having members pointing to the same address, which may be dangerous (possibly resulting in dangling pointers). 在包含成员指针的
struct
上使用此技术(或memcpy()
)将导致两个struct
具有指向相同地址的成员,这可能很危险(可能导致指针悬空)。 If a struct
contains pointers then a deep copy of the struct
is required (allocating memory for the member pointers and copying the pointed to objects in the source struct to the allocated memory in the destination struct
). 如果一个
struct
包含指向则的深层副本struct
是必需的(分配用于所述构件指针存储器和复制尖锐物体在源结构到目标分配的存储器struct
)。
Also, in this case temp
is superfluous and you just assign values directly to the members of a
. 另外,在这种情况下,
temp
是多余的,您只需将值直接分配给a
的成员。
void init(dynArr* a)
{
dynArr temp;
temp.value1 = (double)(rand()) * rand() / rand();
temp.value2 = rand()%100;
temp.value3 = rand()%1000;
printf("In init(): value1: %14.5f, value2: %6d, value3: %6d\n",
temp.value1, temp.value2, temp.value3);
a = &temp; // THIS LINE
}
temp
is a local variable with automatic storage duration. temp
是具有自动存储持续时间的局部变量。 It ceases to exist when init
returns. 当
init
返回时,它不再存在。
Fortunately, the pointer a
that is made to point to temp
also ceases to exist, so init
doesn't create dangling pointers for your programme. 幸运的是,指向
temp
的指针a
也不再存在,因此init
不会为程序创建悬挂的指针。
struct
s are assignable, so if the argument points to valid memory, you can just initialise the pointee with struct
是可分配的,因此,如果参数指向有效内存,则可以使用
*a = temp;
That would be okay for 这样就可以了
init(&(a1[i]));
since &a1[i]
points to allocated memory, but not for 因为
&a1[i]
指向分配的内存,但不是
init(a2[i]);
since a2[i]
is an uninitialised pointer. 因为
a2[i]
是未初始化的指针。
If you want to allocate memory in init
, you'd have to pass a dynArr**
, but then you can't use it for the first case. 如果要在
init
分配内存,则必须传递dynArr**
,但在第一种情况下不能使用它。 So you ought to allocate memory to a2[i]
before calling init
for the second case. 因此,您应该在为第二种情况调用
init
之前将内存分配给a2[i]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.