Friday, 15 March 2013

C - declaring int array inside struct -


In C, I have defined the structure shown below, and want to start it inline. (Neither the field inside the structure nor the array fuse will change after initialization). The code works fine in the first block.

  struct foo {int bar; Int * some_array; }; Fu Fu typedef structure; Int tmp [] = {11, 22, 33}; Structure fu fuses [] = {{123, tmp}};   

However, I do not really need the TMP area. Actually, it will disrupt my code (this example is somewhat simpler) Therefore, instead I want to declare values ​​of some-array inside the declaration for fuses . I can not find the correct syntax, though. The field some-array should be differently defined?

  int tmp [] = {11, 22, 33}; [F1] [F2] [=] {{123, tmp}, // works {222, {11, 22, 33}}, // {222, new int [] {11, 22, 33}} Does not compile, // is not compiled {222, (int *) {11, 22, 33}}, // does not compile {222, (int []) {11, 22, 33}}, / / Compiles, values ​​in the wrong array};    

  int * some_array;   

Here, some_array is actually an indicator, not an array. You can define it like this:

  struct foo {int bar; Int some_array [3]; };   

One more thing, typedef struct Foo Foo's full point; instead of Foo instead of struct Foo . And you can use Type Typefuff like this:

  typedef struct Foo {int bar; Int some_array [3]; } Foo;    

No comments:

Post a Comment