I want to initialize a 2D character array "table" as one of two predefined 2D character arrays called "A" and "B" by using a straightforward void function "assignTable". However, while the array get the right values inside "assignTable" the assigned values seemingly do not carry over into the main function. I suspect that there is some problem with the pointers.
Could you please tell me what I am doing wrong?
#include <stdio.h>
#include <stdlib.h>
char A[10][10] = {
{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'},
{'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'},
{'U', 'V', 'W', 'X', 'Y', 'Z', '.', ',', '!', '?'},
{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'},
{'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'},
{'U', 'V', 'W', 'X', 'Y', 'Z', '.', ',', '!', '?'},
{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'},
{'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'},
{'U', 'V', 'W', 'X', 'Y', 'Z', '.', ',', '!', '?'},
{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'}
};
char B[10][10] = {
{' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
{' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
{' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
{' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
{' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
{' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
{' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
{' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
{' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'},
{' ', 't', 'a', 'b', 'c', 'f', 'g', 'z', 'j', 'm'}
};
void printTable(int rows, int columns, char table[rows][columns])
{
for (int i = 0; i < rows; i = i + 1)
{
for (int j = 0; j < columns; j = j + 1)
printf("%c", table[i][j]);
printf("\n");
}
printf("\n");
}
void asssignTable(char* table, char* table_identity)
{
if (table_identity[0] == 'A')
table = A;
else if (table_identity[0] == 'B')
table = B;
printf("In the function ""assignTable"":\n"); // does work
printTable(10, 10, table);
}
int main()
{
char (*table)[10];
asssignTable(&table, "A");
printf("In main :\n"); // does not work
printTable(10, 10, table);
return 0;
}