danteDev danteDev - 1 year ago 148
C Question

static member(2d array) of structure (2d array) in C

I got a question regarding to static member of structure
Here is the scenario:


  • Need 2d array variable to retain its values even after exited from the function in another C files. Subsequent call from main()-in another C file, to the function will use that last value and continue to do calculation to produce and update new values(retained also). So, I I think I need to use static for that array.

  • The 2D array is member of an 2D structure. I need 2D structure for identity purposes for the 2d array. Lets say I got identity[row][column] with member[5][5], I need statically define the member throughout the call from main(). But static for structure member is not allowed in C as I notice.



code i am trying on:

//in function.h

#define row 2
#define column 5
int function(int rowNUM);

//in function.c
int function(int rowNUM)
{
typedef struct {
static int member[5][5];
int y[5][5];
int forg;
} mystruct;

mystruct identity[row][column];// declare the identity as structure array

int columnNUM;
int c;
int d;

//----try to initialize member to 1
for (columnNUM=0;columnNUM<column;columnNUM++)
{
for (c=0;c<5;c++)
{
for(d=0;d<5;d++){
identity[rowNUM][columnNUM].member[c][d]=1;
}
}
}


//----try to update member--- The values should retain for subsequent call from main
for (columnNUM=0;columnNUM<column;columnNUM++)
{
for (c=0;c<5;c++)
{
for(d=0;d<5;d++){
identity[rowNUM][columnNUM].member[c][d]=1+identity[rowNUM][columnNUM].member[c][d]; // to update member new value
}
}
}
}


// in main.c

main()
{
function(1);
function(2);// member here is in different identity from function (1)
function(1);//member should retain its value from function(1)
function(2);//member should retain its value from function(2)
}


Any other suggestion is welcomed if this is not the way to achieve the goal.
I am quite new in programing Please help me on this.

Answer Source

You have defined a 2d array of structs, which is really hard to statically initialize because each struct contains matrices of ints. Also, you can't have only a member of a struct static, the whole structure has to be defined static.

The easiest way I can think of doing what you want is defining an static matrix of mystructs and a helper variable to initialize it just on the first call, ie,

//in function.c
#include <stdbool.h>

int function(int rowNUM)
{
    typedef struct {
        int member[5][5];
        int y[5][5];
        int forg;
    } mystruct;

    // helper flag    
    static bool first_call = true;

    // this is a matrix of (row x cols) mystructs
    static mystruct identity[row][col];

    if (first_call) {
        /* initialize the matrix however you want
           BUT remember to set first_call to false!
        */
        first_call = false;
    }

    // rest of function ...
}

If you don't want to keep the values of the y and forg members (what does forg stand for? is it a typo?), you should split the struct in two, but I can't see why would you want to keep some values of the struct and discard others unless they are temporal variables.

If y and forg are temporal varaibles, you should definitely split your struct definition in two.

Another way of doing that is passing the pointer to the matrix as a parameter to your function, so you can maintain state without using a static variable

int function(int rowNUM, mystruct **identity)

But note that, although most of the times arrays decay to pointers (you can find more information in similar questions to this one), they are not the same thing.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download