In main() wird die Variable "Array" als
erklärt
char array[50][50];
Dies ist ein 2500 Byte Datenstück. Wenn main() "array" übergeben wird, ist es ein Zeiger auf den Anfang dieser Daten. Es ist ein Zeiger auf ein char erwartet in Reihen von 50
Noch() in Funktion print organisiert werden, erklären Sie
char **array
„Array“ hier ist ein Zeiger auf einen „char * Zeiger“ .
@Lucus Vorschlag von "Leere print (char array [] [50], int SIZE)" funktioniert, mit der Ausnahme, dass es nicht generisch ist, dass Ihre SIZE Parameter muss 50.
Idee: Niederlage (yeech) die Art des Parameter-Array in print()
void printarray(void *array, int SIZE){
int i;
int j;
char *charArray = (char *) array;
for(j = 0; j < SIZE; j++){
for(i = 0; i < SIZE; i ++){
printf("%c ", charArray[j*SIZE + i]);
}
printf("\n");
}
}
Eine elegantere Lösung ist das "array" in main() ein Array von Zeigern zu machen.
// Your original printarray()
void printarray(char **array, int SIZE){
int i;
int j;
for(j = 0; j < SIZE; j++){
for(i = 0; i < SIZE; i ++){
printf("%c ", array[j][i]);
}
printf("\n");
}
}
// main()
char **array;
int SIZE;
// Initialization of SIZE is not shown, but let's assume SIZE = 50;
// Allocate table
array = (char **) malloc(SIZE * sizeof(char*));
// Note: alternative syntax
// array = (char **) malloc(SIZE * sizeof(*array));
// Allocate rows
for (int row = 0; row<SIZE; row++) {
// Note: sizeof(char) is 1. (@Carl Norum)
// Shown here to help show difference between this malloc() and the above one.
array[row] = (char *) malloc(SIZE * sizeof(char));
// Note: alternative syntax
// array[row] = (char *) malloc(SIZE * sizeof(**array));
}
// Initialize each element.
for (int row = 0; row<SIZE; row++) {
for (int col = 0; col<SIZE; col++) {
array[row][col] = 'a'; // or whatever value you want
}
}
// Print it
printarray(array, SIZE);
...
Leere print (char array [] [50], int SIZE) – Lucas
Während es möglich ist, dies zu tun, sind Sie besser dran, es zu einem 1D-Array konvertiert und unter Verwendung von 'j * SIZE + i' um es zu indizieren. – Dave
@Dave warum? ............................... (Just Filler) – kotlomoy