I have problem solving this problem, so if anyne had a similar problem it would help me a lot.
short y[2][3]={{0123},{0x12345}},*p=y[1];
printf("01:%x\n", y);
printf("02:%x\n", p);
printf("03:%x\n", sizeof(y));
printf("04:%x\n", sizeof(y[0]));
printf("05:%x\n", sizeof(&y[0]));
printf("06:%x\n", sizeof(*p));
printf("07:%x\n", sizeof(p ));
printf("08:%x\n", *p );
printf("09:%x\n", *p);
return 0;
Can anyone explain to me why the printout is like this?
01:61ff10
02:61ff16
03:c
04:6
05:4
06:2
07:4
08:2345
09:0
My opinion:
01:Prints the address where the array y begins.
02:Prints the address of the pointer, which points to the second element of the array. Since we have 2 * 3 elements that are of type short, each subsequent element of the zero element will increase by 6.
03:Since we have 2 * 3 elements, which is equal to 6, but the elements of the type are short, so it will print hexadecimal c
04:the number of elements in the zero position is 3, but they are of the short type, so it prints 6
05:prints the sizeof addresses of the first element of the array which is 4
06:I don't know why it prints 2 here
07:Prints the sizeof of the pointer address which is 4, it will increase after printing
08:I do not understand
09:I do not understand
Can anyone explain why it prints like this?
CodePudding user response:
OK, let's see:
- #01: The address of
y. - #02: The value of
p, which holds the address ofy[1], which is the second element of typeshort[3]. The size of ashortis apparently 2 on your system, so the offset to #01 is 6. - #03: The size of the array
y,2 * 3 * sizeof (short)give 12, in hexc. - #04: The size of the element
y[0], which is of typeshort[3]. 6, as you found. - #05: The size of the address of
y[0], and apparently the size of an address is 4 on your system. - #06: The size of the object that
ppoints to. This is ashort, so 2. - #07: The size of the expression
p, which is an address, so 4. And no,pis not incremented, since the expression is not evaluated. - #08: The value of the object that
ppoints to, which isy[1][0]. Since the initializing value of0x12345is aninttoo big to be stored in ashort, it is truncated to0x2345. After reading the value,pis incremented. - #09: The element
ppoints to, which isy[1][1]. It was initialized to 0.
Notes:
You should have got warnings from your compiler:
- The mentioned initializer is truncated.
- The format for pointers/addresses is
%p. - The type of the result of
sizeofmight not match the format%x.
You should take warnings seriously, they are always a hint that you most probably made an error.
CodePudding user response:
N6) Sizeof(*p) is size of datatype pointed by p. p is pointer to short: so 2 bytes.
N8) p is pointer to short, it`s pointing to array element y[1][0]. y[1] and y[1][0] have the same memory address.
All array elements are short, 0x12345 truncates to 0x2345 upon array initialisation. So output is 2345. Also, p increases pointer value to point to next short y[1][1].
N9) Because of p in step N8, pointer now points to y[1][1], which was initialised to 0 (by default, because init value not provided) - output is 0.
