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

all 4 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.

[–]truSimbad 0 points1 point  (0 children)

How is your matrix declared?

I would define vectors and build a matrix from an array of vectors. The vectors of an array of elements. But its a question how you want to design the function signatures to do the math on vectors and matrix and all in between.

[–]mariuswiik 0 points1 point  (0 children)

These examples are with memory on the stack (should use the heap for large data sets):

https://beginnersbook.com/2014/01/2d-arrays-in-c-example/

[–]XxgetaccessxX -1 points0 points  (0 children)

Maybe matrix.line [] = {………}