3

Im trying to create a 2d array for multiple data types but it seems to not be accepting the char data type. Why is this?

struct {
union {
    int ival;
    float fval;
    char cval[50];
} val;
} as[120][4];


as[0][1].val.cval = "Testtttt";  ***This does not work***
as[1][1].val.ival = 3;  ***This works***

4 Answers 4

5

You are in , thus you should use string.h when it comes to string handling!


Change this:

as[0][1].val.cval = "Testtttt";

to this:

strcpy(as[0][1].val.cval, "Testtttt");

by using strcpy(), instead of the assignment operator (this would work in , not in ).


Of course, alternative functions exist, such as strncpy()* and memcpy().

Moreover, since C string handling seems new to you, you must read about null terminated strings in C.


*Credits to @fukanchik who reminded me that

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

3 Comments

did you mean strncpy ?
No @fukanchik, but that's an option too, I updated my answer, is it better now? :)
strcpy is just no-go because buffer overrun is possible.
3

In C, this code

as[0][1].val.cval

can not be assigned to. Per the C Standard, 6.3.2.1 Lvalues, arrays, and function designators:

Except when it is the operand of the sizeof operator, the _Alignof operator, or the unary & operator, or is a string literal used to initialize an array, an expression that has type "array of type" is converted to an expression with type "pointer to type" that points to the initial element of the array object and is not an lvalue.

Without getting too in-depth into the C Standard, an lvalue is something you can assign something to. Thus, this code

as[1][1].val.ival

represents an lvalue and you can assign 3 to it.

The reason an array can't be assigned to is because it decays to "an expression with type ‘‘pointer to type". In other words, a bare array like

as[0][1].val.cval

is treated as the address of the array.

And the address of the array is where it is and is not something that can be assigned to.

Comments

2

Your val.cval members are arrays of char. String literals also represent arrays of char. C does not support whole-array assignment, regardless of the type of the array elements.

You can copy the contents of one array to another in various ways. strcpy() will do it for null-terminated arrays of char. memcpy() and / or memmove() will do it more generally, and of course you can always write an element-by-element copy loop.

Comments

1

You cannot copy the contents of one array to another using the = operator; you must use a library function like strcpy (for strings) or memcpy (for anything else), or you must assign each element individually:

as[0][1].val.cval[0] = 'T';
as[0][1].val.cval[1] = 'e';
as[0][1].val.cval[2] = 's';
...
as[0][1].val.cval[7] = 't';
as[0][1].val.cval[8] = 0;

Remember that in C, a string is a sequence of character values terminated by a 0-valued byte. Strings (including string literals like "Testtttt") are stored as arrays of char, but not all arrays of char store a string.

1 Comment

That's an actual good different answer from the first one here, upvoted.

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.