web-dev-qa-db-fra.com

Comment passer un tableau 2D par pointeur en C?

J'apprends C et j'ai du mal à faire passer le pointeur d'un tableau 2D vers une autre fonction qui imprime ensuite le tableau 2D. Toute aide serait appréciée. 

int main( void ){
    char array[50][50];
    int SIZE;

    ...call function to fill array... this part works.

    printarray( array, SIZE );
}

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" );
    }
}
32
user1362058

char ** ne représente pas un tableau 2D - ce serait un tableau de pointeurs sur des pointeurs. Vous devez changer la définition de printarray si vous voulez lui passer un tableau 2D:

void printarray( char (*array)[50], int SIZE )

ou équivalent:

void printarray( char array[][50], int SIZE )
24
Carl Norum

Dans main (), la variable "array" est déclarée comme 

char array[50][50];

Il s’agit d’une donnée de 2500 octets. Quand le "tableau" de main () est passé, c'est un pointeur sur le début de cette donnée. C'est un pointeur sur un caractère qui devrait être organisé en rangées de 50.

Pourtant, dans la fonction printarray (), vous déclarez

 char **array

"tableau" est ici un pointeur sur un "pointeur char *".

La suggestion @Lucus de "void printarray (char array [] [50], int SIZE)" fonctionne, sauf qu'elle n'est pas générique car votre paramètre SIZE doit être 50.

Idée: Defeat (yeech) le type du tableau de paramètres dans printarray ()

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" );
    }
}

Une solution plus élégante consiste à transformer le "tableau" dans main () en un tableau de pointeurs.

// 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);
...
5
chux

C99 prenant en charge les tableaux de taille dynamique, le style suivant est tout simplement plus pratique pour passer un tableau à 2 dimensions:

void printarray( void *array0, int SIZE ){
    char (*array)[SIZE] = array0;
    int i;
    int j;
    for( j = 0; j < SIZE; j++ ){
        for( i = 0; i < SIZE; i ++){
            printf( "%c ", array[j][i] );
        }
        printf( "\n" );
    }
}
1
Simon Woo

Vous pouvez facilement passer le tableau 2D en utilisant un double pointeur.

  void printarray( char **array, int n)
  {
     int i, j;
     for(i=0; i<n; i++ )
     {
         for(j=0; j<n; j++)
         {
            printf("%c ", array[i][j] );
         }
        printf( "\n" );
     }
  }

  int main()
  {
      int n = 2;
      int i, j;

      char **array = (char **) malloc(n * sizeof(char*));

      for (i=0; i<n; i++) 
      {
        array[i] = (char *) malloc(n* sizeof(char));
      }

     for (i=0; i<n; i++)
     {
       for (j=0; j<n; j++)
       {
           scanf("%c ", &array[i][j]);
       }
     }

     printarray(array, n);

     return 0;
  }

Code complet: Ideone

0
rashedcs