Using a 2D Array in Function: Error - c

I tried to set values in 2D array and Print it with Function.
but I got trouble to send the Array to the Function.
#include <stdio.h>
void SetArr(int (*arr)[], int n);
void PrintfArr(int (*arr)[], int n);
int main()
{
int arr[100][100];
for(int i=0; i<100; i++)
for(int j=0; i<100; j++)
arr[i][j]=0; //initiallize Array
int size;
printf("input Size : "); scanf("%d", &size);
SetArr(arr, size);
PrintfArr(arr, size);
return 0;
}
void SetArr(int (*arr)[], int n)
{
int i, j;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
arr[i][j]=i+j; // Error! "Subscript of Pointer to imcomplete~
}
}
void PrintArr(int (*arr)[], int n)
{
int i, j;
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf("%d", arr[i][i]); // Error! "Subscript of Pointer to imcomplete~
}
printf("\n");
}
As you see, both of functions got same problem while using "arr[][]"

In the declaration of the function, array must be mentioned with its size (length). This is what the compiler means when it says "incomplete type": it needs the size to understand how to access its elements.
In order to make size available to the compiler, it should go first in the function's declaration:
void SetArr(int n, int (*arr)[n])
Here arr is technically a "pointer to an array of n elements", but in C it's customary to treat a pointer as an array, so arr can be treated as "an array of arrays of n elements", which is the same as a 2-D array.
You might want to mention both dimensions of the array:
void SetArr(int n, int arr[n][n])
This is equivalent to the first declaration, but seems clearer.

Declare the functions like
void SetArr( int ( *arr )[100], int n );
void PrintfArr( int ( *arr )[100], int n );
You may not declare the functions as it is shown in the answer that you marked as the best
Consider the following code where instead of 100 I am using 3 for the array dimensions.
If you will print the array in function PrintArr and in main you will get different results!
#include <stdio.h>
void SetArr( size_t n, int (*arr)[n] )
void PrintArr( size_t n, int (*arr)[n] )
int main(void)
{
int arr[3][3];
size_t n = 2;
SetArr( n, arr );
PrintArr( n, arr );
printf( "\n" );
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < n; j++ ) printf( "%2d", arr[i][j] );
printf( "\n" );
}
return 0;
}
void SetArr( size_t n, int (*arr)[n] )
{
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < n; j++ ) arr[i][j] = i * n + j;
}
}
void PrintArr( size_t n, int (*arr)[n] )
{
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < n; j++ ) printf( "%2d", arr[i][j] );
printf( "\n" );
}
}
The program output is
0 1
2 3
0 1
3 1
As you see the first output does not coincide with the second output. You would get the correct result if the functions were declared as I pointed in the beginning of the post. For example
#include <stdio.h>
void SetArr( int (*arr)[3], size_t n );
void PrintArr( int (*arr)[3], size_t n );
int main(void)
{
int arr[3][3];
size_t n = 2;
SetArr( arr, n );
PrintArr( arr, n );
printf( "\n" );
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < n; j++ ) printf( "%2d", arr[i][j] );
printf( "\n" );
}
return 0;
}
void SetArr( int (*arr)[3], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < n; j++ ) arr[i][j] = i * n + j;
}
}
void PrintArr( int (*arr)[3], size_t n )
{
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < n; j++ ) printf( "%2d", arr[i][j] );
printf( "\n" );
}
}
The program output is
0 1
2 3
0 1
2 3
As you can see in this case the both outputs coincide.

Related

How to pass a 2d array of struct elements to a function in C [duplicate]

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

Segmentation fault by using pointer to pointer [duplicate]

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

Passing matrix as a parameter

I am new to c, I am using CUDA compiler. I want to declare five matrices, from a[1][1] to a[5][5]. Initialize them with random values. Print these values. I have shown the whole code I wrote. I face problems
Passing matrix as parameter
Initialize a matrix (should be constant)
#include <stdio.h>
#include <stdlib.h>
void printMatrix(int **a, int rows, int cols)
{
for(int i=0; i<rows; i++){
for (int j=0; j<cols; j++){
printf("%d\t", a[i][j] );
}
printf("\n");
}
}
int main() {
int N;
for (int n=0; n<=5; n++)
N = n;
int a[N][N];
for(int i=0; i<N; i++)
for (int j=0; j<N; j++){
a[i][j] = rand() % 256;
}
printf("Matrix A\n");
printMatrix(a, 10, 10);
}
}
This works fine if i define N on the top. If I did that, I couldn't change N value using for loop. How can I correct.
For starters there is a typo in the program. You omitted the open brace after the loop statement
for (int n=0; n<=5; n++)
^^^^
N = n;
int a[N][N];
//...
There should be
for (int n=0; n<=5; n++) {
^^^^
N = n;
int a[N][N];
//...
Array a is a variable length array. Its dimension may not be equal to 0. So the variable n must to start from 1 as it is written in the assignment
for (int n=1; n<=5; n++) {
^^^^
This function call
printMatrix(a, 10, 10);
^^ ^^
does not make sense because the number 10 has nothing common with the array.
And the function declaration
void printMatrix(int **a, int rows, int cols);
^^^^^^^
is invalid. There is a mismatch between the argument type and the parameter type and there is no implicit conversion from one type to another type.
The program can look like
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void printMatrix( size_t rows, size_t cols, int a[][cols] )
{
for ( size_t i = 0; i < rows; i++ )
{
for ( size_t j = 0; j < cols; j++ ) printf( "%3d ", a[i][j] );
printf( "\n" );
}
}
int main( void )
{
const size_t N = 5;
const int UPPER_VALUE = 256;
srand( ( unsigned int )time( NULL ) );
for ( size_t n = 1; n <= N; n++ )
{
int a[n][n];
for ( size_t i = 0; i < n; i++ )
{
for ( size_t j = 0; j < n; j++ ) a[i][j] = rand() % UPPER_VALUE;
}
printf( "Matrix A[%zu][%zu]:\n", n, n );
printMatrix( n, n, a );
printf( "\n" );
}
return 0;
}
Its output might be
Matrix A[1][1]:
117
Matrix A[2][2]:
57 216
50 233
Matrix A[3][3]:
42 117 215
177 218 26
202 81 163
Matrix A[4][4]:
205 178 157 2
229 165 93 94
91 160 39 205
26 242 131 69
Matrix A[5][5]:
147 248 126 107 42
103 149 160 62 70
122 89 17 203 252
222 125 154 224 98
63 61 192 155 222
If the compiler does not support variable length arrays then you have to allocate the arrays dynamically. For example
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void printMatrix( int **a, size_t rows, size_t cols )
{
for ( size_t i = 0; i < rows; i++ )
{
for ( size_t j = 0; j < cols; j++ ) printf( "%3d ", a[i][j] );
printf( "\n" );
}
}
int main( void )
{
const size_t N = 5;
const int UPPER_VALUE = 256;
srand( ( unsigned int )time( NULL ) );
for ( size_t n = 1; n <= N; n++ )
{
int **a = malloc( n * sizeof( int * ) );
for ( size_t i = 0; i < n; i++ )
{
a[i] = malloc( n * sizeof( int ) );
for ( size_t j = 0; j < n; j++ ) a[i][j] = rand() % UPPER_VALUE;
}
printf( "Matrix A[%zu][%zu]:\n", n, n );
printMatrix( a, n, n );
printf( "\n" );
for ( size_t i = 0; i < n; i++ ) free( a[i] );
free( a );
}
return 0;
}
for (int n=0; n<=5; n++){ /* <--missing { which causes N to be 6 after last iteration */
//your code
}
You missed a { after first for loop in main that's why int a[N][N] and other loops are not inside its body (which you probably want "changing value of N ")
It seems that the number of matrices is constant, so just #define it at the top. Without touching your printMatrix method, you can have a main body as follows:
#define N 5
int main(int argc, char ** argv)
{
int **data[N]; // Array of length N of int **, where each int ** will store a matrix.
for (int i = 0; i < N; i++) {
int matrixSize = i + 1; // creating the ith matrix
data[i] = malloc(matrixSize * sizeof *data[i]);
for (int j = 0; j < matrixSize; j++) {
data[i][j] = malloc(matrixSize * sizeof *data[i][j]);
for (int k = 0; k < matrixSize; k++) {
data[i][j][k] = rand() % 256;
}
}
}
// Printing the first one
printMatrix(data[0], 1, 1);
// don't forget to loop again to free the buffers allocated...
return 0;
}

Find error in functions (sum, subtract, multiply) with square matrix in C

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void matrixRandomFill(int size, int size2, int matrix[size][size2]) {
srand(time(NULL));
for ( int i = 0; i < size; i++ ) {
for ( int j = 0; j < size2; j++ ) {
matrix[i][j] = rand() % 9;
}
}
}
void matrixSum(int size, int size2, int matrix1[size][size2], int matrix2[size] [size2], int matrixSum[size][size2]) {
for ( int i = 0; i < size; i++ ) {
for ( int j = 0; j < size2; j++ ) {
matrixSum[i][j] = matrix1[i][j] + matrix2[i][j];
}
}
}
void matrixSubstract(int size, int size2, int matrix1[size][size2], int matrix2[size] [size2], int matrixSubstract[size][size2]) {
for ( int i = 0; i < size; i++ ) {
for ( int j = 0; j < size2; j++ ) {
matrixSubstract[i][j] = matrix1[i][j] - matrix2[i][j];
}
}
}
void matrixMultiply(int size, int size2, int matrix1[size][size2], int matrix2[size] [size2], int matrixMultiply[size][size2]) {
for ( int i = 0; i < size; i++ ) {
for ( int j = 0; j < size2; j++ ) {
matrixMultiply[i][j] = matrix1[i][j] * matrix2[i][j];
}
}
}
void matrixPrint(int size, int size2, int matrix[size][size2]) {
for ( int i = 0; i < size; i++ ) {
for ( int j = 0; j < size2; j++ ) {
printf("%2d ", matrix[i][j]);
}
printf("\n");
}
}
int main() {
int size;
printf("Enter matrix size NxN: ");
scanf("%d", &size);
int matrix1[size][size];
int matrix2[size][size];
int matrixSum[size][size];
int matrixSubstract[size][size];
int matrixMultiply[size][size];
matrixRandomFill(size, size, matrix1);
matrixRandomFill(size, size, matrix2);
printf("Printing first matrix:\n");
matrixPrint(size, size, matrix1);
printf("--------------------------------------\n");
printf("Printing second matrix:\n");
matrixPrint(size, size, matrix2);
printf("--------------------------------------\n");
printf("Printing matrix1 + matrix2:\n");
matrixPrint(size, size, matrixSum);
printf("--------------------------------------\n");
printf("Printing matrix1 - matrix2:\n");
matrixPrint(size, size, matrixSubstract);
printf("--------------------------------------\n");
printf("Printing matrix1 * matrix2:\n");
matrixPrint(size, size, matrixMultiply);
printf("--------------------------------------\n");
return 0;
}
the functions look normal, but i keep getting something like this:
Printing matrix1 + matrix2:
0 0 0
0 0 0
0 0 0
or
Printing matrix1 - matrix2:
-1761243347 32767 -1761341440
32767 0 0
0 0 0
looks like some kind of segmentation fault, but i can't figure out where i made a mistake.
The problem is you are just printing the matrices but not performing any operations before it.
// You forgot to call matrix sum function here.
printf("Printing matrix1 + matrix2:\n");
matrixPrint(size, size, matrixSum);
printf("--------------------------------------\n");
Similarly for subtraction, multiplication operations as well.
Your functions matrixSum etc are never called.
As others said, You never actually evaluate matrixSum and the rest, also I found that your local(to main) variables that have the same name as your functions hide your functions, so they will be no longer available past those variable declarations.
The way I see your another problem is(by example):
int iwilldosomething(int a, int b) /*function iwilldosomething */
int main(void)
{
int iwilldosomething; /* local variable iwilldosomething */
iwilldosomething(2, 3); /* here you will get error, because main can see iwilldosomething and will tell that iwilldosomething is not a function */
.
.
.

How to pass a 2D array by pointer in C?

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

Resources