0

I want to define a struct to emulate a vector, containing an array of double vec and an integer N representing how many entries are valid.

I want to set all the entries of the array to be zero before filling N of them with non zero values. I tried this code:

typedef struct {
  int N;
  double vec[1000];
} Vector ;

int main()
{
  int N1;
  double vec1[1000] = { 0 };
  Vector vector;
  vector.vec = { 0 };
  return 0;
}

The compiler accepts the initialization of vec1, but not of vector.vec. I get an error that says:

so.c:11:16: error: expected expression
  vector.vec = { 0 };
               ^
1 error generated.

What am I doing wrong?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
ecjb
  • 5,169
  • 12
  • 43
  • 79
  • The error message shown does not match the code. Please make a proper [mre]. Also: just because you called your struct `Vector` doesn't make it a vector, or a question that should be tagged that way, or a word that helps explain the question in the title; and the issues here aren't because of `typedef`, but instead are entirely related to using the `struct`. – Karl Knechtel Jan 29 '23 at 11:17
  • I updated the code sorry about that – ecjb Jan 29 '23 at 11:20
  • @user17732522 the question is tagged [c] and the code is intended to be written in C as far as I can tell. – Karl Knechtel Jan 29 '23 at 11:20
  • @KarlKnechtel Oops. I think I got confused by the `vector`. – user17732522 Jan 29 '23 at 11:21
  • Yes I want to define a typedef or a struct in C (not C++) – ecjb Jan 29 '23 at 11:21
  • You can only initialize an array with `{...}` in the same statement where it is defined and not in a separate statement. I.e. `int a[3]={...};`, but `int a[3]; a={...};` is not. – wohlstad Jan 29 '23 at 11:23
  • thank you @wohlstad. So what would be the code? – ecjb Jan 29 '23 at 11:24
  • 1
    Instead, try `Vector vector = { 0 };` – Weather Vane Jan 29 '23 at 11:25
  • "I want to define a typedef or a struct " It's important to understand how the `typedef struct` idiom works in C. Reference: https://stackoverflow.com/questions/252780 – Karl Knechtel Jan 29 '23 at 11:26
  • 1
    For your `Vector` you can do as suggested above by @WeatherVane. But in general if you want to initialize as array you already declared, you need to assign the elements in a loop. – wohlstad Jan 29 '23 at 11:26
  • You can also zero a block of memory with `memset()`. Such as `memset(vector.vec, 0, sizeof vector.vec);` – Weather Vane Jan 29 '23 at 11:27
  • As for the question, it is plainly a duplicate: see https://stackoverflow.com/questions/330793 for the reference canonical. (I already voted to close as needing debugging details before that was fixed, so now I can't formally propose the duplicate.) – Karl Knechtel Jan 29 '23 at 11:27
  • 1
    Like @WeatherVane suggested, `Vector vector = { 0 };` or `Vector vector = {.N = 0 };` if you want to be explicit about it. `Vector vector = { 0, {fill some} };` would also work. If the elements are not contiguous from the start: `Vector vector = {0, {[1] = 3.141, [3] = 6.6}};` etc. – Ted Lyngmo Jan 29 '23 at 11:30
  • 1
    Thank you @WeatherVane and wohlstad and Ted Lyngmo for your very helpful comments! :) – ecjb Jan 29 '23 at 11:32
  • Presumably `vector.N` holds the number of elements so it isn't clear why you would want to do that. – Weather Vane Jan 29 '23 at 11:33

0 Answers0