打印联合的不同类型数组的输出
我创建了一个联合,并将不同类型的数组放入其中。我按顺序打印了输出,但我确实不明白一些要点。
1)为什么我的char数组的长度总是8,即使内容不同?里面只有“你好”。为什么输出是“Cats rock!”当我尝试第二次打印时。我没有在数组中放入类似的内容。
2)再次长度问题。我所有数组的长度都是 8,甚至是 union 的长度。为什么?
3)我的最后一个问题是为什么当我第二次尝试打印时双数的值会改变。
我正在向您发布我的代码并输出我得到的结果。抱歉,帖子很长,但我真的很困惑。
char: hello, 8
double: 5.557111111111111, 8
int: 1937006915 1668248096 8555, 8
char: Cats rock!
double: 0.000000000000000
int: 1937006915 1668248096 8555
size of union: 8
我的代码
#define NUM1 5.557111111111111
#define NUM2 1937006915
#define NUM3 1668248096
#define NUM4 8555
#include <stdio.h>
/*created an union*/
typedef union {
char * array1;
double num;
int * array2;
} myunion;
int main(int argc, char ** argv)
{
/* declaring union variable */
myunion uni;
/* creating my string */
char strarray[] = "hello";
/* declare an int array*/
int numarray[] = { NUM2, NUM3, NUM4 };
/* assign the string to the char pointer in the union */
uni.array1 = strarray;
/* print the union and the sizeof of the pointer */
printf("char: %s, %zu\n", uni.array1,sizeof(uni.array1));
/* assign NUM1 to the double part of union */
uni.num = NUM1;
/* print the double and its sizeof */
printf("double: %10.15f, %zu\n", uni.num, sizeof(uni.num));
/* assign array2 of union to the numarray */
uni.array2 = numarray;
/* print the values and the sizeof */
printf("int: %d %d %d, %zu\n", uni.array2[0], uni.array2[1], uni.array2[2], sizeof(uni.array2));
/* print the char array, double and int array */
printf("\nchar: %s \ndouble: %10.15f \nint: %d %d %d\n",uni.array1, uni.num, uni.array2[0], uni.array2[1], uni.array2[2]);
/* print the size of the union */
printf("size of union: %zu\n", sizeof(uni));
return 0;
}
I created an union and I put different types of array inside of it. I printed outputs in an order and I really didn't understand some points.
1) Why is my char array's length always 8 even the content is different? There is only "hello" inside of it. And why does the output is "Cats rock!" when I try to print for second time. I didn't put anything like that inside array.
2)Again length problem. Lenghts of all my arrays are 8 even the length of union. Why?
3) My last question is why double number's value changed when I try to print for the second time.
I'm posting you my code and out put that I get. Sorry about long post, but I'm really confused.
char: hello, 8
double: 5.557111111111111, 8
int: 1937006915 1668248096 8555, 8
char: Cats rock!
double: 0.000000000000000
int: 1937006915 1668248096 8555
size of union: 8
my code
#define NUM1 5.557111111111111
#define NUM2 1937006915
#define NUM3 1668248096
#define NUM4 8555
#include <stdio.h>
/*created an union*/
typedef union {
char * array1;
double num;
int * array2;
} myunion;
int main(int argc, char ** argv)
{
/* declaring union variable */
myunion uni;
/* creating my string */
char strarray[] = "hello";
/* declare an int array*/
int numarray[] = { NUM2, NUM3, NUM4 };
/* assign the string to the char pointer in the union */
uni.array1 = strarray;
/* print the union and the sizeof of the pointer */
printf("char: %s, %zu\n", uni.array1,sizeof(uni.array1));
/* assign NUM1 to the double part of union */
uni.num = NUM1;
/* print the double and its sizeof */
printf("double: %10.15f, %zu\n", uni.num, sizeof(uni.num));
/* assign array2 of union to the numarray */
uni.array2 = numarray;
/* print the values and the sizeof */
printf("int: %d %d %d, %zu\n", uni.array2[0], uni.array2[1], uni.array2[2], sizeof(uni.array2));
/* print the char array, double and int array */
printf("\nchar: %s \ndouble: %10.15f \nint: %d %d %d\n",uni.array1, uni.num, uni.array2[0], uni.array2[1], uni.array2[2]);
/* print the size of the union */
printf("size of union: %zu\n", sizeof(uni));
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sizeof(uni.array1)
在 64 位平台上始终为 8,在 32 位平台上始终为 4,因为它采用的是指针的大小,不知道您认为该指针后面可能有多少数据。数组类似。您指向数组的 C 指针是“哑”的,并且不了解数组有多大——您需要单独传递该信息。这涵盖了您的问题第 1 部分和第 2 部分。我们希望在这里回答具体问题,因此请随时将您的第三个询问移至单独的帖子。
sizeof(uni.array1)
is always 8 on 64-bit platforms and 4 on 32-bit ones because it is taking the size of a pointer, not knowing how much data you believe might be behind that pointer. Similar for the arrays. C pointers which you make point to an array are "dumb" and do not understand how large the array is--you need to pass that information around separately.This covers your question parts 1 and 2. We like to answer specific questions here, so feel free to move your third inquiry to a separate post.
您的联合实际上并不包含数组,它只包含两个指针(其中之一)。机器上指针的大小是 8。如果您希望数组成为联合的一部分,请这样做
数组必须具有固定长度。这就是编译时类型的本质。
Your union doesn't actually contain the arrays, it just contains (one of) two pointers. The size of a pointer on your machine is 8. If you want the arrays to be part of the union, do it this way
The arrays have to have fixed length. That's the nature of compile-time types.