0

I am fairly new to C and have a bit of a trouble using an array as a part of a struct. I have a struct defined as follows:

typedef struct
{
 int row;
 int col;
 int * puzzle[5][5]
} data;

Later on in a function, I am trying to use this struct as follows:

void foo(int grid[5][5])
{
 data *puz = (data *)malloc(sizeof(data));
 puz->row = 3;
 puz->col = 2;
 puz->puzzle = grid;  // causes an error
 }

The last statement produces an error "assignment to expression with array type". I understand that this has something to do with the equal sign and array data structure, but I am not sure how to resolve this. Any help is greatly appreciated. Thank you.

6
  • You should define your pointer int (*puzzle)[5][5] this way. Commented Sep 23, 2018 at 20:16
  • Show the call to foo. Commented Sep 23, 2018 at 20:16
  • 1
    Argh, closed as a wrong duplicate. Your code as 2 errors - you presumably want a 2d array of ints - int puzzle[5][5], then use memcpy (careful with the size, sizeof puz->puzzle!) Commented Sep 23, 2018 at 20:16
  • @kiranBiradar doesn't help much, because a) probably that's not what was actually intended, b) converting int *grid[5] to int (*puzzle)[5][5] is a bit tricky for a newbie Commented Sep 23, 2018 at 20:19
  • 1
    kiran Biradar, fixed it, thank you so much! Commented Sep 23, 2018 at 20:21

2 Answers 2

2

This ...

    int * puzzle[5][5]

... declares puzzle as an array of 5 arrays of 5 pointers to int each. Colloquially, we more often call such a thing a 5 x 5 array, but the key thing you should appreciate is that the underlying scalar element type is a pointer type. That does not match the scalar element type (int) of the parameter of function foo, and it is anyway unlikely to be what you want. (Note also that your code omits the required semicolon after that member's declaration.)

It seems likely that the structure member should instead be declared as ...

int puzzle[5][5];

... i.e. a 2D array of int. C does not have whole-array assignment, so in that case you need a different way to copy the function parameter into the new structure. You could write a set of nested loops, but it is more straightforward to use memcpy():

memcpy(&puz->puzzle, grid, sizeof(puz->puzzle));

In the unlikely event that you want your structure to contain a pointer to an array, then you might declare it either as

int (*puzzle)[5];

or

int (*puzzle)[5][5];

or even

int *puzzle;

Which of those you chose would affect how you assign to the member and how you access the int array elements. Since I don't think any of these are what you're actually after, I leave the details as an exercise.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you so much, this completely fixed it! I upvoted your answer but since I don't post too often here and my reputation here is <15 it did not show. Thank you again!
@AlexanderNenartovich, I'm glad to have been able to help. For your part, as the person who asked the question, you are empowered to "accept" one answer by clicking on the check mark to the left of its score, regardless of your rep. Doing so indicates that the question has been answered to your satisfaction, and will give a reward to the author of that answer. This is separate from and independent of up or downvoting.
Just did that. Thanks!
0

I think you should call the function with a pointer to the array, and modify the function signature accordingly. But if you want to copy the array rather than point to it, you should perform something like memcpy.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.