0
struct move {
int left;
int right;
int up;
int down;
};

struct move moves[CONFIG_VARIABLE_X];

Number of moves array is a configuration variable, it can be set to any value by developer.

Now, all the moves[x].right alone should be inited with value 1. I know that I can write a funciton and loop through and init the right member with 1, but is there any way to init this particular value in the definition itself above?

RRON
  • 1,037
  • 3
  • 12
  • 32
  • Short answer: no. There are tricks to (sort of) work around that: search for e.g. "c init struct member with identical value". –  Nov 11 '16 at 02:52
  • As a GCC-specific extension over standard C, the second answer to the duplicate question, in combination with designated initializer syntax for the structure element, will allow you do what you want. Without using the GCC extension (or an equivalent in some other compiler), there isn't a way to do it. – Jonathan Leffler Nov 11 '16 at 04:09

1 Answers1

0

Hope it helps you.

#include <stdio.h>
#define MAX 8

struct move {
int left;
int right;
int up;
int down;
};

int main()
{
        struct move moves[MAX]={[0 ... MAX-1].right = 1};

        printf("...%d\n",moves[0].left);
        printf("...%d\n",moves[1].right);
        printf("...%d\n",moves[2].right);
        printf("...%d\n",moves[3].right);
        printf("...%d\n",moves[4].right);
        printf("...%d\n",moves[5].right);
        printf("...%d\n",moves[6].right);
        printf("...%d\n",moves[7].right);
        return 0;
}
Sumit Gemini
  • 1,836
  • 1
  • 15
  • 19