-2

In the below code, I'm trying to assign part of the array to another array which results in a compile error but when I send the same array in the function arguments, there is no error and it is working.

void foo(char arr[])
{
    printf("arr = %s\n",arr);
}


int main(void) {

    char a[] = "Hello world";
    char b[15];

    b = a+6; //  error: assignment to expression with array type

    foo(a+6);

    printf("a = %s\n",a);

    printf("b = %s\n",b);

    return EXIT_SUCCESS;
}

Can someone explain what is happening here. If I use pointer like this, it is working.

char *b;
b = a+6;
4
  • 1
    It's invalid in C to do an array assignment like that. Can only set individual elements in the array. "when I send the same array in the function arguments, there is no error and it is working". That's because it is the assignment and not the addition that is the problem. The "working" part of the code does not do any assignment. Commented Jul 30, 2021 at 6:02
  • 3
    Does this answer your question? Why can't I assign an array to another array in C Commented Jul 30, 2021 at 6:03
  • 1
    arrays and pointers are different Commented Jul 30, 2021 at 6:03
  • 1
    See also this question for more about the (non) assignability of arrays. Commented Jul 30, 2021 at 16:22

2 Answers 2

1

The assignment b = a+6 cannot work, as b is not a pointer, it is the address of an array, so you cannot assign to it. When you declared it as char *b; then the assignment works, as you can assign to a pointer.

Your function call will work because you are passing a pointer (to the 6th element of array a), and your function foo() takes a char* as its argument.

Consider what the assembly language will look like: your variable a will become some memory (let's say beginning at 0x1000) holding the following:

0x1000 'H'
0x1001 'e'
0x1002 'l'
...
0x1009 'l'
0x100a 'd'
0x100b '\0'

Your variable b will be a further 15 bytes of memory, beginning at 0x100c.

But a and b are not pointers, they are simply the values 0x1000 and 0x100c. b = a+6 is the equivalent of saying 0x100c = 0x1000 + 6 which is clearly nonsense.

When you declare b as char *b; you end up with some number of bytes (4 or 8 most likely--32 or 64 bits) which will hold the address of a character. So while b will still be at 0x100c, it now holds an address of memory, so when you write b = a+6 you will be writing the number 0x1006 into the memory at 0x100c.

Understanding the difference between pointers and arrays is one of the most confusing parts of C, but if you think about what is happening at the machine level it's pretty easy to keep things straight.

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

1 Comment

Now I understand why assigning to 'b' is giving an error. I was confused with 'char arr[]' in the function parameters foo() with the 'char b[];'. so char arr[] and char* arr are the same in the function parameter.
1

Arrays only decay to pointers when used, but they are still arrays not pointers. C does not allow assigning the arrays (it would require copying the content of the array).

You need to use one of the memory copy functions or write your own one.

  strcpy(b, a + 6);
  memcpy(b, a + 6, strlen(a + 6) + 1);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.