0

I tried this code, but it doesn't work. I want to send only the element p of the struct grid, not the whole struct to the function.

struct grid{ 
  int p; 
  int s; 
}

void create(int a[9][9]); 
{ 
  a[0][2] = 4; 
}

int main() 
{
  struct grid a[9][9]; 
  create(a.s); 
}
6
  • 2
    int, int[][9] are two different types. You can not intermix. To access the members of the array, you need to use [] operator. Commented Dec 24, 2013 at 16:46
  • 5
    Select a language and stick to it. C and C++ are two languages, count them. Commented Dec 24, 2013 at 16:46
  • 4
    There is no easy way to do what you are asking for in C without re-structuring your program. Commented Dec 24, 2013 at 16:48
  • 1
    C-style 2D arrays are a nightmare hack to be desperately avoided. Commented Dec 24, 2013 at 16:51
  • 2
    An element of array of structs is a struct. An element of a struct may be an integer. You cannot select an array of integers out of an array of structs. Commented Dec 24, 2013 at 16:55

4 Answers 4

1

As others pointed out, what you need cannot be done in that way. One way to resolve the issue is to transform the matrix before passing it to the method:

using namespace std;

struct grid{ 
    int p; 
    int s; 
};

void create(int a[9][9])
{ 
    a[0][2] = 4; 
}

void ExtractS(struct grid a[9][9], int s[9][9])
{
    for (int i = 0; i < 9; i++)
        for (int j = 0; j < 9; j++)
            s[i][j] = a[i][j].s;
}

int main() 
{
    struct grid a[9][9]; 
    int s[9][9];
    ExtractS(a, s);
    create(s);
}

But this opens more questions than it answers:

  • Data would have to be returned back into original matrix of structures, or otherwise change made by create is lost
  • This method is very inefficient because it copies all the data

Maybe you should revise the design and make create function receive matrix of grid elements.

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

1 Comment

You could make the transform take a matrix of references to int instead if just int, that way changes aren't lost, but scoping can be an issue.
1

Initial Note: void create(int a[9][9]) turns in fact into
void create(int a[][9]) or void create(int (*a)[9])

You may want void create(int (&a)[9][9]).

As you have struct grid { int p; int s; }; and struct grid a[9][9];

You should initialize like this:

void create(grid (&g)[9][9])
{
    g[0][2].s = 4;
}

or change your struct as

struct grid {
    int p[9][9];
    int s[9][9];
};

And then have

void create(grid &g)
{
    g.s[0][2] = 4;
}

or

void create(int (&a)[9][9])
{
    a[0][2] = 4;
}

that you call respectively create(g) and create(g.s).

Comments

1

You basically have three choices, switch to two matrices, put the dimensions in the struct, or have create() take an array of struct:

Two arrays:

void create(int a[9][9]) 
{ 
  a[0][2] = 4; 
}

int main() 
{
  int p[9][9];
  int s[9][9]; 
  create(s); 
}

-- or -- put the dimensions in the struct:

struct grid{ 
  int p[9][9]; 
  int s[9][9]; 
};

void create(int a[9][9])
{ 
  a[0][2] = 4; 
}

int main() 
{
  struct grid a; 
  create(a.s); 
}

-- or -- Pass an array of struct

struct grid{ 
  int p; 
  int s; 
};

void create(struct grid[9][9] * a) 
{ 
  (*a)[0][2].s = 4; 
}

int main() 
{
  struct grid a[9][9]; 
  create(&a); 
}

You cannot cherry pick elements out of an array or matrix of structs

Comments

0

You are passing an integer to a function which expects a pointer to 2D array as an argument,

The prototype for the function to accept the member variable s of structure grid as argument should be as follows,

void create(int a); 

Also there is an unwanted semicolon in your program in the function definition.

void create(int a[9][9]); <-- No need
{ 
    a[0][2] = 4; 
}`
`

2 Comments

but I think he actually wants to send in the 9x9 of esses, not just one number, well spotted on the ; tho.
There is a missing semicolon in his struct definition as well.

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.