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..
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.