Diamond
Diamond

Reputation: 1

Printing 2d dynamically allocated array using pointer to pointer method

Here in this code I am trying to dynamically allocate memory to a 2d array and print it The problem I face here is I cant get the output properly lets assume my input is an 2d array of size 2*2 [1,2,3,4] but the output printed is [1,1,1,1].

#include <stdio.h>
#include <stdlib.h>

int output_func(int row, int col, int *ptr)
{
    printf("\nThis is your first matrix: \n\n");
    for (int i = 0; i < row; i++)
    {
        printf("\t  |");
        for (int j = 0; j < col; j++)
        {
            printf(" %d ", *ptr);
        }
        printf("|\n");
    }
}

int main()
{
    int **ptr;
    int row, col;
    int i, j;

    printf("enter the no of rows for the matrix: ");
    scanf("%d", &row);
    printf("enter the no of col for the matrix: ");
    scanf("%d", &col);

    ptr = (int **)malloc(row * sizeof(int *));
    for (int i = 0; i < row; i++)
    {
        ptr[i] = (int *)malloc(col * sizeof(int));
    }

    // input of first matrix
    printf("\nEnter the elements for first matrix :\n");
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            printf("\t A[%d,%d] = ", i, j);
            scanf("%d", &ptr[i][j]);
        }
    }

    output_func(row, col, *ptr);
    return 0;
}

Upvotes: 0

Views: 60

Answers (2)

4386427
4386427

Reputation: 44274

If you want a function to print the array allocated using an array of pointers to arrays of int (also known as a jagged array), you must pass the pointer to the array of pointers.

In other words - this is wrong:

int output_func(int row, int col, int *ptr)
                                  ^^^^^^^^
                                  A int-pointer but you want
                                  a pointer to a pointer to int
                                  which is "int **ptr"

So do

int output_func(int row, int col, int **ptr)
{
    printf("\nThis is your first matrix: \n\n");
    for (int i = 0; i < row; i++)
    {
        printf("\t  |");
        for (int j = 0; j < col; j++)
        {
            printf(" %d ", ptr[i][j]);
        }
        printf("|\n");
    }
}

and call it like:

output_func(row, col, ptr);

Upvotes: 1

linuxias
linuxias

Reputation: 324

#include <stdio.h>
#include <stdlib.h>

// int* ptr -> int** ptr
void output_func(int row, int col, int** ptr)
{
    printf("\nThis is your first matrix: \n\n");
    for (int i = 0; i < row; i++)
    {
        printf("\t  |");
        for (int j = 0; j < col; j++)
        {
            // *ptr -> ptr[i][j]
            printf(" %d ", ptr[i][j]);
        }
        printf("|\n");
    }
}

int main()
{
    int** ptr;
    int row, col;
    
    printf("enter the no of rows for the matrix: ");
    scanf("%d", &row);
    printf("enter the no of col for the matrix: ");
    scanf("%d", &col);

    ptr = (int**)malloc(row * sizeof(int*));
    for (int i = 0; i < row; i++)
    {
        ptr[i] = (int*)malloc(col * sizeof(int));
    }

    // input of first matrix
    printf("\nEnter the elements for first matrix :\n");
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            printf("\t A[%d,%d] = ", i, j);
            scanf("%d", &ptr[i][j]);
        }
    }

    output_func(row, col, ptr);
    return 0;
}

Upvotes: 1

Related Questions