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?

2
  • Short answer: no. There are tricks to (sort of) work around that: search for e.g. "c init struct member with identical value". Commented Nov 11, 2016 at 2: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. Commented Nov 11, 2016 at 4:09

1 Answer 1

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; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.