1

I am tryng to fill and struct within struct with array. Is there any way I can do it? I tried loops and simply just putting array instead of hard numbers but no luck. Here code:

typedef struct
{
    int *tokens;
    char *name;
    int numTokens;
    int index;
} Pile;

typedef struct
{
    Pile N;
    Pile W;
    Pile E;
    Pile S;
} Game;

int main(void)
{
    //tokens
    int north[MAX_TOKENS], west[MAX_TOKENS], east[MAX_TOKENS], south[MAX_TOKENS];
    // sizes of token value fields
    int north_size = 0, west_size = 0, east_size = 0, south_size = 0;
    int check = read_tokens(north, west, east, south, &north_size, &west_size, &east_size, &south_size);
    //I read them and then store backwards.
    int *arrays[4] = {north, west, east, south};
    int sizes[4] = {north_size, west_size, east_size, south_size};
    store_values_backwards(arrays, sizes);
    //Then I need to send arrays with the numbers I read into this
        Pile N = {(int[]){*north}, "N", north_size, 0};
        Pile W = {(int[]){*west}, "W", west_size, 0};
        Pile E = {(int[]){*east}, "E", east_size, 0};
        Pile S = {(int[]){*south}, "S", south_size, 0};

Instead of hard coded numbers like:

  Pile N = {(int[]){-66, -52, 109}, "N", 3, 0};
  Pile W = {(int[]){78}, "W", 1, 0};
  Pile E = {(int[]){118,146,46,77}, "E", 4, 0};
  Pile S = {(int[]){67,159,-13}, "S", 3, 0};
Adrian
  • 59
  • 7
  • You are aware that `*north` and `north[0]` are equivalent, are you? You tried to cast an ordinary int to array... – Aconcagua Dec 19 '22 at 09:26
  • I never rly understand pointers. Visual Code types an error I put * and pray to god. But thanks. – Adrian Dec 19 '22 at 09:33
  • Then start learning, pointers are *essential* in C... What they actually do is containing memory addresses, so with `int n; int* p = &n;` the pointer `p` contains the address of `n`. As long as you haven't understood try visualising on paper: Draw a rectangle for some memory content, draw a series of to represent arrays. Then have yet another rectangle representing the pointer. Whenever you do an assignment, draw an arrow from the pointer to the variable you just assigned (for arrays, point to its first element!). That might help getting an impression of. – Aconcagua Dec 19 '22 at 09:45
  • A [good C book](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) should give you some fine explanation as well. – Aconcagua Dec 19 '22 at 09:47

1 Answers1

0

The first member of the struct is a pointer to int (int *tokens;) and you want to initialize it using an array of ints.

Then

Pile N = {&north[0], "N", north_size, 0};

or simply

Pile N = {north, "N", north_size, 0};

should work.

Take a look to the C-FAQ:

A reference to an object of type array-of-T which appears in an expression decays (with three exceptions) into a pointer to its first element; the type of the resultant pointer is pointer-to-T.

That is, whenever an array appears in an expression, the compiler implicitly generates a pointer to the array's first element, just as if the programmer had written &a[0]. (The exceptions are when the array is the operand of a sizeof or & operator, or is a string literal initializer for a character array.

David Ranieri
  • 39,972
  • 7
  • 52
  • 94