-2

Hello I am very new to C and I have a simple question. Why does the second method of assigning a string to char name2[] not work? It causes a compilation error saying "Array type 'char[20]' is not assignable".


int main() {
    char name[20] = "Alex";

    char name2[20];
    name2 = "Alex"; //error!

}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
zach
  • 37
  • 6
  • Please use `strcpy(name2, "Alex");`. The `=` can only be used in an initialisation of the array variable definition. – Weather Vane Oct 05 '22 at 18:10

3 Answers3

1

Arrays do not have the assignment operator. Instead you need to copy the string from one array to another like

#include <string.h>

//...

strcpy( name2, name );

Or just to copy the string literal using the same function strcpy

strcpy( name2, "Alex" );

On the other hand, you can assign the address of the string literal to a pointer like

char *name2;
name2 = "Alex";

In this case you may not change the string literal using the pointer.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
1

Why does the second method of assigning a string to char name2[] not work? It causes a compilation error saying "Array type 'char[20]' is not assignable".

There is no "second method" presented. This ...

    char name[20] = "Alex";

... does not demonstrate an assignment, but rather an initialization. The = within is not functioning as the assignment operator, but rather as part of the syntax for specifying the initial value that name will take. And yes, this is a common source of confusion for newcomers to C.

On the other side, it is not possible to assign to whole arrays in C (this also is a common source of confusion for newcomers). This is the reason for the compilation error. You can copy the contents of one array into another with, for example, strcpy() or memcpy(), but there are almost no operators that accept arrays as operands. C arrays have more surprises to offer you, too, but I won't spoil them.

Once you understand C's idiosyncratic treatment of arrays, I think you'll see that it's internally consistent. Until then, however, you would do well to be alert whenever you see or use an array.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
-2

Assuming you want to define a different string, and not just copy the previous one (assuming you used the same name as an example), you can use the scanf command:

#include <stdio.h>

int main()
{
    char name[20] = "alex";
    char name2[20];
    
    printf("Type the name: \n");
    scanf("%s", name2);
    printf("name is %s. name2 is %s",name, name2);

    return 0;
}

In this case it will be required an user input (you will need to write the name on the terminal).

Felipe_SC
  • 1
  • 3
  • I don't see how this answers their question; they don't show anything about user input, just copying a string. – Thomas Jager Oct 05 '22 at 18:37
  • Since he is new to C programming I supposed he want to define the string in a way other than while declaring it and used the same string as an example. As pointed by John Bollinger, there is no second method for doing this (unless it is required the user input). – Felipe_SC Oct 05 '22 at 18:45