2

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?

3
  • 1
    Note: when printing a size_t value, use "%zu" rather than "%lu". Commented Jul 10, 2015 at 4:30
  • Note: using char a2[] = "abcd"; char a1[] = "efg"; likely would helped lead to a deeper understanding. Commented Jul 10, 2015 at 4:49
  • Thanks chux! I have to try that case. Commented Jul 14, 2015 at 7:52

2 Answers 2

6

As it has already been pointed out when you do the following you will get undefined behavior:

for(i =0; i < sizeof(a2); i++){
    a2[i]=a1[i];
    printf("%c\n", a2[i]);
}

This is because the size of a2 is 5 and the size of a1 is 4 so you have accessed the fifth element of a1 (a1[4]) which is going beyond the array. So when you do a2[4] = a1[4], which is what the last iteration of your for loop is doing you could be assigning any value to a2[4] since the value of a1[4] is not well defined. So even though a2[4] is a valid element in the array you have assigned something to it that is not well defined and when you print a2[4] you could be printing anything. See the last paragraph for an explanation of why you are most likely getting b.

There are a few things to remember with this. First arrays are zero based indexed and second strings in C are null terminated so sizeof("aa") is actually 3 not 2.

Since your program has undefined behavior it could print anything as any value could be in a1[4]. Having said that if the compiler placed the array a2 right after a1 you would consistently see b printed as you assigned the value b to a2[0] in the first iteration of your loop. This is just one possible explanation for why you are seeing a b. The compiler does not have to place the arrays in this position and in fact on my machine it does not place the arrays in this position. This is what it means when behavior is undefined in C.

Sign up to request clarification or add additional context in comments.

Comments

1

"why a[4] value is "b"" is incorrect. (Certainly OP means a2 here.)

Note that the size of a2 is printed as 5. The following does not access outside the array bounds of a2. Instead code is simply printing the null character '\0' initially, and then ....

char a2[] = "aaaa";
....
printf("%c\n",a2[4]);

Code enters undefined behavior with the below loop in the last iteration as a2[4] = a1[4]; accesses a1 outside its 4 char size. So the rest of code behavior is undefined.

char a1[] = "bbb";
...
for(i =0; i < sizeof(a2); i++){
    a2[i]=a1[i];
    printf("%c\n", a2[i]);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.