0

I have this matrix that holds a 4x4 array of double:

typedef struct {
 double data[4][4];
} mat;

And here I declare my mat

mat m1;

And i want to initialize my mat with zeros:

double data[4][4] = { { 0,0,0,0 },{ 0,0,0,0 },{ 0,0,0,0 },{ 0,0,0,0 } };
MAT_A.data = data;

And got this error:

expression must be a modifiable lvalue

And:

'=': left operand must be l-value

I also tried this:

MAT_A->data = data;

EDIT:

Another question is how to initiate mat ?

asked Dec 21, 2017 at 13:28
1

4 Answers 4

2

The problem is that you cannot assign an array to a struct, nor an array to an array. The C syntax does not allow it.

And if you want to initialize a variable, you should not use assignment. These are different things; MAT_A.data = data; is assignment. This is initialization:

mat m =
{
 .data = { { 0,0,0,0 },{ 0,0,0,0 },{ 0,0,0,0 },{ 0,0,0,0 } }
};

As for assignment, what you can do is assign a struct to a struct, for example by using a compound literal:

m = (mat) 
{
 .data = { { 0,0,0,0 },{ 0,0,0,0 },{ 0,0,0,0 },{ 0,0,0,0 } }
};
answered Dec 21, 2017 at 13:48
Sign up to request clarification or add additional context in comments.

Comments

1

The problem is not in your initialization of the array data, but in the following assignment:

MAT_A.data = data;

In order to copy an array you need to copy the elements of the array individually:

for (int i = 0; i < 4; i++)
 for (int j = 0; j < 4; j++)
 MAT_A.data[i][j] = data[i][j];
answered Dec 21, 2017 at 13:32

Comments

1

i want to initiate my mat with zeros:

Then you could simply code:

mat m1 = {0};

or

double data[4][4] = {0};

(and {} instead of {0} would also practically work, even if non-standard)

and you could even use memset to clear the zone (with <string.h> standard header):

double data[4][4];
memset (&data, 0, sizeof(data));

or

memset (&m1, 0, sizeof(m1));

BTW, optimizing compilers like GCC (invoked as gcc -O2 -march=native) are likely to generate in practice the same machine code in all cases, if m1 or data is an automatic variable (on your call stack).

However, assignments of arrays (m1.data = data;) is illegal (because arrays decay into pointers).

And your title is wrong: C has no multi-dimensional arrays, only (mono-dimensional) arrays (perhaps arrays of arrays, arrays of structures, etc...).

answered Dec 21, 2017 at 13:32

3 Comments

I take it you mean {0} since {} would be invalid C.
And i need to initiate first my mat ? how can i do that ?
Didn't I answer to that in my answer? Please improve first your question by giving an minimal reproducible example. Then I could improve my answer !
0

You could explicitly initialize the array as follows.

for (int i = 0; i < 4; i++)
{
 for (int j = 0; j < 4; j++)
 {
 MAT_A.data[i][j] = 0;
 }
}
answered Dec 21, 2017 at 13:33

1 Comment

This is assignment, not initialization.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.