[英]Passing an argument in the function to turn struct values into an array
I passed my function void Assign_numbers
to void Adding
. 我将函数void Assign_numbers
传递给void Adding
。 When I change it to a pointer and compile it, the first array value returns the same value from number.a
in void Assign_numbers
. 当我将其更改为一个指针和编译,所述第一阵列值返回从相同的值number.a
在void Assign_numbers
。 The other value in the array starting from ptr[1]
gives a entirely different number. 从ptr[1]
开始的数组中的另一个值给出了一个完全不同的数字。 Can you please help me so the array is able to output the other assigned numbers. 能否请您帮我,以便阵列能够输出其他分配的数字。 Please don't change the formatting. 请不要更改格式。
#include <stdlib.h>
#include <stdio.h>
#define Max 6
struct Numbers
{
double a,b,c,d,e,f;
};
void Adding(struct Numbers *ptr)
void Assign_numbers()
{
struct Numbers number;
number.a=45.78;
number.b=81.45;
number.c=56.69;
number.d=34.58;
number.e=23.57;
number.f=78.35;
Adding(&number);
}
void Adding(struct Numbers *ptr)
{
int i =0;
for(i;i<Max;i++)
{
ptr[i];
printf("%.2f\n",ptr[i]);
}
}
int main()
{
Assign_numbers();
return 0;
}
In the Adding
function the variable ptr
points to a single structure, essentially an arry of a single element. 在Adding
函数中,变量ptr
指向单个结构,实质上是单个元素的数组。 When you do eg ptr[1]
you try to access the second element in this one-element array, which is out of bounds and will lead to undefined behavior . 例如,当执行ptr[1]
您尝试访问此单元素数组中的第二个元素,该元素超出范围,将导致未定义的行为 。
There's simply no way you can treat a structure in standard C like an array. 根本无法将标准C中的结构像数组一样对待。
There are hacks though. 虽然有黑客 。 For example using a union with the structure as one member and an array as another, but that relies on the compiler not adding any padding between the members of the structure. 例如,使用将结构作为一个成员的联合,将数组作为另一个成员,但是这依赖于编译器在结构的成员之间不添加任何填充。 It's neither portable nor recommended. 它既不便携也不推荐。
Type of the pointer (in your case struct Numbers
) defines offset of the elements in the array. 指针的类型(在您的情况下为struct Numbers
)定义数组中元素的偏移量。 For example if you have array of some type T
use of a[i]
would mean take value of T
from the address a + i * sizeof(T)
or simplier. 例如,如果你有一些类型的数组T
使用的a[i]
将意味采取值T
从地址a + i * sizeof(T)
或simplier。
Note: To be true a[i]
would be converted to *(a + i)
, and this is the reason why i[a]
works just fine. 注意:确实, a[i]
将被转换为*(a + i)
,这就是i[a]
运行良好的原因。 See With arrays, why is it the case that a[5] == 5[a]? 请参见使用数组,为什么a [5] == 5 [a]? question. 题。
Now you have struct Numbers *ptr
and use ptr[0]
that makes it *(ptr + 0 * sizeof(struct Numbers))
, which leads you to your struct in you memory. 现在,您有了struct Numbers *ptr
并使用ptr[0]
使其成为*(ptr + 0 * sizeof(struct Numbers))
,这将您带到内存中的结构。 However the use of ptr[1]
would lead you to *(ptr + 1 * sizeof(struct Numbers))
, but there is no data on this address in memory and this causes undefined behavior. 但是,使用ptr[1]
会导致*(ptr + 1 * sizeof(struct Numbers))
,但是内存中此地址上没有数据,这会导致未定义的行为。
What you wanted to do is to iterate through your struct with double*
, for example 您想要做的是使用double*
遍历结构,例如
void Assign_numbers()
{
struct Numbers number;
number.a=45.78;
number.b=81.45;
number.c=56.69;
number.d=34.58;
number.e=23.57;
number.f=78.35;
Adding((double*)&number);
}
void Adding(double* ptr)
{
int i = 0;
for(i; i < Max; i++)
{
ptr[i];
printf("%.2f\n",ptr[i]);
}
}
prints 版画
45.78
81.45
56.69
34.58
23.57
78.35
Another way is to use union
. 另一种方法是使用union
。 See C method for iterating through a struct's members like an array? 看到C方法像数组一样遍历结构的成员吗? for example. 例如。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.