henry said:
But not of compilable code.
int assy[3]={1,2,3};
char kb[3]={'a','b','c'};
printf("address of arry of int = %p, %p, %p\n",assy,assy+1,assy+2);
printf("address of arry of char = %p, %p, %p\n",kb,kb+1,kb+2);
These are both wrong. The specifier "%p" is for printing
pointers-to-void. None of the arguments you supply are of that
type. See my code below.
And I got following print out and it is not following sizeof(int)*:
What is that '*' doing in the line above, and how would you know that
"it is not following sizeof(int)"?
The lines below suggest just the opposite.
address of arry of int = 0xbf9af580, 0xbf9af584, 0xbf9af588
address of arry of char = 0xbf9af57d, 0xbf9af57e, 0xbf9af57f
Copy the code below and run it on your implementation. If you do not
have the specifier "%zu", you can, _with this example_, safely replace
it with %u and cast the arguments to (unsigned). I have provided
after the code the output on my implementation. If you study the code
and the output a light should flash and your confusion disappear.
#include <stdio.h>
int main(void)
{
int assy[] = { 1, 2, 3 };
char kb[] = { 'a', 'b', 'c' };
char *cptr;
size_t i;
cptr = kb;
printf("'kb' is an array char[], with sizeof kb = %zu\n"
"It has sizeof kb / sizeof *kb = %zu elements, "
"each of size *kb = %zu\n"
"I have set the char * cptr = kb.\n", sizeof kb,
sizeof kb / sizeof *kb, sizeof *kb);
for (i = 0; i < sizeof kb / sizeof *kb; i++)
printf("&kb[%zu] = %p, kb+%zu = %p, cptr+%zu*sizeof *kb = %p\n",
i, (void *) &kb
,
i, (void *) (kb + i),
i, (void *) (cptr + i * sizeof *kb));
putchar('\n');
cptr = (char *) assy;
printf("'assy' is an array int[], with sizeof assy = %zu\n"
"It has sizeof assy / sizeof *assy = %zu elements, "
"each of size *assy = %zu\n"
"I have set the char * cptr = (char *)assy.\n", sizeof assy,
sizeof assy / sizeof *assy, sizeof *assy);
for (i = 0; i < sizeof assy / sizeof *assy; i++)
printf
("&assy[%zu] = %p, assy+%zu = %p, cptr+%zu*sizeof *assy =
%p\n",
i, (void *) &assy, i, (void *) (assy + i), i,
(void *) (cptr + i * sizeof *assy));
return 0;
}
[Output]
'kb' is an array char[], with sizeof kb = 3
It has sizeof kb / sizeof *kb = 3 elements, each of size *kb = 1
I have set the char * cptr = kb.
&kb[0] = dff99, kb+0 = dff99, cptr+0*sizeof *kb = dff99
&kb[1] = dff9a, kb+1 = dff9a, cptr+1*sizeof *kb = dff9a
&kb[2] = dff9b, kb+2 = dff9b, cptr+2*sizeof *kb = dff9b
'assy' is an array int[], with sizeof assy = 12
It has sizeof assy / sizeof *assy = 3 elements, each of size *assy = 4
I have set the char * cptr = (char *)assy.
&assy[0] = dff9c, assy+0 = dff9c, cptr+0*sizeof *assy = dff9c
&assy[1] = dffa0, assy+1 = dffa0, cptr+1*sizeof *assy = dffa0
&assy[2] = dffa4, assy+2 = dffa4, cptr+2*sizeof *assy = dffa4