I am learning C and am having trouble passing the pointer of a 2D array to another function that then prints the 2D array. Any help would be appreciated.
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" );
}
}
char **
doesn't represent a 2D array - it would be an array of pointers to pointers. You need to change the definition of printarray
if you want to pass it a 2D array:
void printarray( char (*array)[50], int SIZE )
or equivalently:
void printarray( char array[][50], int SIZE )
In main()
, the variable "array" is declared as
char array[50][50];
This is a 2500 byte piece of data. When main()
's "array" is passed about, it is a pointer to the beginning of that data. It is a pointer to a char expected to be organized in rows of 50.
Yet in function printarray()
, you declare
char **array
"array" here is a pointer to a char *pointer
.
@Lucus suggestion of void printarray( char array[][50], int SIZE )
works, except that it is not generic in that your SIZE parameter must be 50.
Idea: defeat (yeech) the type of parameter array in 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" );
}
}
A more elegant solution is to make the "array" in main()
an array of pointers.
// 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: cleaner alternative syntax
// array = malloc(sizeof *array * SIZE);
// 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: cleaner alternative syntax
// array[row] = malloc(sizeof(**array) * SIZE);
}
// 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);
...
Since C99 supports dynamic-sized arrays, the following style is simply more convenient to pass a 2-dim array:
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" );
}
}
none of the answers here were what I was looking for, so I'm posting my simple solution to the problem
#include <iostream>
using namespace std;
void example(int* mat, int dim0, int dim1){
for(int i = 0; i < dim0; ++i) {
for(int j = 0; j < dim1; ++j) {
auto cur_index = i * dim1 + j;
cout<< *(mat + cur_index) << endl;
}
}
}
int main()
{
const int dim0 = 3;
const int dim1 = 2;
int mat[dim0][dim1];
for(int i = 0; i < dim0; ++i) {
for(int j = 0; j < dim1; ++j) {
mat[i][j] = i * dim1 + j;
}
}
example(&(mat[0][0]), dim0, dim1);
return 0;
}
You can easily pass the 2d array using double pointer.
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;
}
Full Code : Ideone
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.