Question

this is my structure struct lookup

{
   char action;
   int state;
};

value of rows and columns are known but they are read from a file.

main()   
{
   // other initialization...then
   lookup* table[rows][columns];
   for (int i = 0; i < rows;i++)
   {    
        for (int j = 0; j < columns;j++)
        {   
             table[i][j]=new (lookup);
        }
   }
}

then i assigned values to each element of table now i want to pass this table to another function for further operations say,

void output(lookup* table)
{
     // print values stored in table 
}

how can i pass the table with all its content to output() function from main()?? thanks for help..

Était-ce utile?

La solution

Declare parameter as double pointer (pretend you receive an 1-dimensional array of pointers). Since such array lies in memory contiguously, you can compute position of current element.

void output(lookup** table, int rows, int cols)
{
  lookup* current_lookup = NULL;
  for (int i = 0; i < rows; i++)
  {   
    for (int j = 0; j < cols; j++)
    {
      current_lookup = table[i*cols + j];
      printf("Action: %c, state: %d\n", current_lookup->action, current_lookup->state);
    }
  }
}

You call it by passing first element of an array:

int main()   
{
   lookup* table[rows][columns];

   //....

   output(table[0]);
   return 0;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top