How to pass a 2D array by pointer in C? - 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

Related

Trouble passing a 2d array to function after malloc

I've gotten back into C recently after a long absence, and I can't for the life of me remember how to pass a 2D matrix to a function and use it after mallocating it. I believe I've passed the pointer correctly, but I can't seem to actually access anything in the matrix and can't figure out why.
This is what I have written:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
void matrixOperation(int *arr, int m, int n)
{
printf("in matrixOperation\n ");
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d\n", *((arr+i*n) + j));
}
int main()
{
int i,j,count;
int row, col;
//------------------------------
printf("Number of rows?\n ");
scanf("%d", &row);
printf("Number of columns?\n ");
scanf("%d", &col);
//------------------------------
int* arr[row];
for (i = 0; i < row; i++)
arr[i] = (int*)malloc(col * sizeof(int));
count = 0;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
arr[i][j] = ++count;
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
printf("%d\n",arr[i][j]);
// We can also use "print(&arr[0][0], m, n);"
matrixOperation((int *)arr, row, col);
for (int i = 0; i < row; i++)
free(arr[i]);
return 0;
}
The goal was to have it accept user input for the size of the matrix, fill each index with a count and then pass it to a function which would print it out. However when I try it the print statement just outputs random numbers which I assume are what the pointers are pointing at.
I would like matrixOperation to print out the same thing as the print statement in main and I can't quite figure out what I've done wrong.
Change the function signature and you should be able to use the same piece of code.
void matrixOperation(int **arr, int m, int n)
{
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d\n",arr[i][j]);
}
Call with:
matrixOperation(arr, row, col);
Remember when an array is passed to a function, it "decays" into a pointer. You have an array of pointers. That decays to a pointer to pointers.
You declared an array of pointers:
int* arr[row];
So pass this array to the function.
The function will be declared like:
void matrixOperation(int **arr, int m, int n)
{
printf("in matrixOperation\n ");
int i, j;
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
printf("%d\n", *( *( arr + i ) + j ) );
}
and call the function like:
matrixOperation( arr, row, col );
Though it will be better to define the function like:
void matrixOperation(int **arr, int m, int n)
{
printf("in matrixOperation\n ");
for ( int i = 0; i < m; i++ )
{
for ( int j = 0; j < n; j++ )
{
printf("%d ", *( *( arr + i ) + j ) );
}
putchar( '\n' );
}
}
Pay attention to that this expression with the pointer arithmetic:
*( *( arr + i ) + j )
can be rewritten using the subscript operator the following way:
arr[i][j]
The both expressions are equivalent each other.
As for your call of the function with casting the array designator:
matrixOperation((int *)arr, row, col);
then it is wrong because you do not have one extent of memory. You allocated several extents of memory.

how to dynamically allocate a 2d array with the help of a function in C [duplicate]

This question already has answers here:
Correctly allocating multi-dimensional arrays
(2 answers)
Closed 3 months ago.
void alloc_matrix(int ***mat, int *m, int *n) {
mat = (int **)malloc(*m * sizeof(int *));
for(int i = 0; i < *m; i++)
mat[i] = (int *)malloc(*n * sizeof(int));
for(int i = 0; i < *m; i++)
for(int j = 0; j < *n; j++)
scanf("%d", &mat[i][j]);
for(int i = 0; i < *m; i++)
for(int j = 0; j < *n; j++) {
printf("%d ", mat[i][j]);
printf('\n');
}
}
i wanted to read and allocate the matrix in the same function, but when i call it, nothing will print, i think there is something wrong with the way i used the pointers, but i cant figure out what is the problem
The expressions like &mat[i][j] or mat[i][j] used in the for loops and the expression mat used in the statement that allocates memory for an array of pointers
mat = (int **)malloc(*m * sizeof(int *));
for(int i = 0; i < *m; i++)
mat[i] = (int *)malloc(*n * sizeof(int));
for(int i = 0; i < *m; i++)
for(int j = 0; j < *n; j++)
scanf("%d", &mat[i][j]);
for(int i = 0; i < *m; i++)
for(int j = 0; j < *n; j++) {
printf("%d ", mat[i][j]);
printf('\n');
}
are incorect.
Instead you have to write
*mat = (int **)malloc(*m * sizeof(int *));
for(int i = 0; i < *m; i++)
( * mat )[i] = (int *)malloc(*n * sizeof(int));
for(int i = 0; i < *m; i++)
for(int j = 0; j < *n; j++)
scanf("%d", &( *mat )[i][j]);
for(int i = 0; i < *m; i++)
{
for(int j = 0; j < *n; j++) {
printf("%d ", ( *mat )[i][j]);
}
printf( "\n" );
}
That is the parameter mat has the type int ***. This means that the original pointer of the type int ** is passed to the function by reference indirectly through a pointer to it. Thus you need to dereference the parameter to get an access to the original pointer.
And this call of printf
printf('\n');
where you are incorrectly using the integer character constant '\n' instead of the string literal "\n" should be placed after the inner for loop.
Also there is no sense to declare m and n as pointers. The function could be declared at least like
void alloc_matrix(int ***mat, int m, int n) {
Here is a demonstration program
#include <stdio.h>
#include <stdlib.h>
void alloc_matrix( int ***mat, int m, int n )
{
*mat = ( int ** )malloc( m * sizeof( int * ) );
for (int i = 0; i < m; i++)
( * mat )[i] = ( int * )malloc( n * sizeof( int ) );
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++)
scanf( "%d", &( *mat )[i][j] );
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++) {
printf( "%d ", ( *mat )[i][j] );
}
putchar( '\n' );
}
}
int main( void )
{
enum { M = 2, N = 3 };
int **mat = NULL;
alloc_matrix( &mat, M, N );
for (int i = 0; i < M; i++)
{
free( mat[i] );
}
free( mat );
}
Its output might look like
1 2 3
4 5 6
1 2 3
4 5 6
The first two lines is the user input and the next two lines is the output of elements of the dynamically allocated arrays.

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

Using a 2D Array in Function: Error

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.

Resources