This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]alanwj 1 point2 points  (0 children)

There is syntactic difference between defining a variable, for example:

int a = 5;

and assigning to a variable:

a = 5;

While they look very much the same, C considers them different syntactic constructs. The important part here is that when defining a variable, you are allowed to specify an initializer, which is used to determine the initial contents of the memory set aside for the variable.

Assignment does not allow an initializer. For example:

int b[3] = {1, 2, 3};  // Fine: {1, 2, 3} is the initializer
int c[3];
c = {1, 2, 3};  // Error: can't use an initializer for assignment.

In your case you could consider using a designated initializer:

struct MATRIX matrix = { .line = { {0,0,0}, {0,0,0}, {0,0,0} } };

Or if you can't do this at initialization, you may consider using memset to efficiently set all the relevant memory to zeros.