유니온을 만들었고 그 안에 다른 유형의 배열을 넣었습니다. 나는 출력물을 주문서에 인쇄했는데 몇 가지 사실을 이해하지 못했습니다.다른 유형의 유니온 배열 출력하기
1) 내용이 다른데도 내 char 배열의 길이가 항상 8 인 이유는 무엇입니까? 그 안에는 단지 "안녕"이 있습니다. 왜 출력은 "Cats rock!"입니까? 내가 두 번째로 인쇄하려고 할 때. 나는 내부 배열과 같은 것을 넣지 않았다.
2) 다시 길이 문제. 모든 나의 배열의 길이는 심지어 노동 조합의 길이도 8이다. 왜?
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;
}
노동 조합이 실제로 무엇인지 전혀 알지 못합니까? – Dan