I'm learning C language by my self. When I assign one array value of element to another, (looks) strange thing happens. My Code is below.
int main(void){
int i =0;
char a2[] = "aaaa";
char a1[] = "bbb";
printf("%lu\n",sizeof(a2));
printf("%lu\n",sizeof(a1));
printf("%c\n",a2[4]);
printf("---\n");
for(i =0; i < sizeof(a2); i++){
a2[i]=a1[i];
printf("%c\n", a2[i]);
}
printf("---\n");
printf("%c\n", a2[4]);
return 0;
}
result is below.
5 4 --- b b b b --- b
I have no idea why a[4] a2[4] value is "b".
Firstly, I thought that if I try to compile this code, compiler would through error, but it says OK.
So I show the result and saw the a2[4] element contain "b" character.
How array works in C?
What should I have to learn the notion for the deeper understand of mechanism?
size_tvalue, use"%zu"rather than"%lu".char a2[] = "abcd"; char a1[] = "efg";likely would helped lead to a deeper understanding.