-1

I am new to coding in C and had a question about the "assignment to expression with array type" error. From my understanding (correct me if I'm wrong), using char* in conjunction with malloc() allocates memory on the heap, which we can read and write to. Using a char var[], allocates memory on the stack frame, which we can also read and write to. I thought that these two would be similar to work with, but apparently they are quite different.

Using the code below, I was able to remove the first char in a string on the heap (by incrementing the pointer to the string).

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int main(void) {
    char* test = malloc(20 * sizeof(char));
    strcpy(test, "XHello World!");
    test = &test[1];
    printf("%s\n", test); // Hello World!
}

Trying to do the same operation with a char var[] though, I ran into the "assignment to expression with array type" error. My code is below.

#include <string.h>
#include <stdlib.h>
#include <stdio.h>

int main(void) {
    char test[20] = "XHello World!";
    test = &test[1]; // assignment to array type error
    // test = test + 1 // same error
    // test = *(test + 1); // same error
    // test += sizeof(char); // same error
    printf("%s\n", test);
}

In the above code, I thought that since test is a pointer, and &test[1] is also a pointer (just like in the first example), assignment would work the same. Could someone explain why this isn't the case? Any help would be greatly appreciated!

My actual goal was to extract a string from some brackets (eg, [HELLO]), and I was trying to use the above technique to remove the opening bracket, and test[strcspn(test, ']')] = '\0' to remove the closing bracket. Maybe this is a bad approach entirely?

trincot
  • 317,000
  • 35
  • 244
  • 286
Michael Hoefler
  • 105
  • 2
  • 9
  • 1
    Test is **not** a pointer in your second example - it's an array and its value can't be changed. – Support Ukraine Sep 01 '20 at 16:14
  • 2
    You could achieve the same ouput with `printf("%s\n", &test[1]);` but you cannot reassign an array variable. In any case, the first example is poor practice because it will not be so simple to `free` the memory you allocated. – Weather Vane Sep 01 '20 at 16:16
  • 1
    Also relevant: https://stackoverflow.com/questions/7882735/why-cant-i-assign-arrays-as-a-b/7883036#7883036 – Support Ukraine Sep 01 '20 at 16:19
  • 1
    And: https://stackoverflow.com/questions/61099179/referencing-a-variable-to-an-existing-array – Support Ukraine Sep 01 '20 at 16:22

2 Answers2

2

No, arrays are not pointers and vice-versa.

You cannot assign to an array type, that's not a lvalue.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

Array is not a pointer. Arrays only decay to pointers causing a lots of confusion when people learn C language.

You cant assign arrays in C. You only assign scalar types (integers, pointers, structs and unions) but not the arrays.

Structs can be used as a workaround if you want to copy the array

struct bar 
{
    char c[20];
};

int main(void)
{
    struct bar b = {.c = "Hello World!!"};
    struct bar x;

    x = b;   // it is correct and will copy the whole struct `b` to struct `x`
}
0___________
  • 60,014
  • 4
  • 34
  • 74