0

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;
}
4
  • If you're going to the print multiple dimensions of a pointer-to-pointer array, you need to provide the pointer-to-pointer array; not just the first row. The first hint is that output_func can't access to the superior dimension (because there isn't one, because you never provided one). The second hint is the call in main, which purposely dereferences the ptr var to comply with the inappropriate output_func declaration. While that allows the code to compile, compile-success does not mean it is correct. Commented Jan 30, 2022 at 6:43
  • 3rd hint: check the line printf(" %d ", *ptr); Commented Jan 30, 2022 at 6:50
  • You have a block of code that reads the matrix, and a block of code that prints the matrix. One has scanf("%d", &ptr[i][j]); and the other has printf(" %d ", *ptr);. Can you justify the difference? Commented Jan 30, 2022 at 7:42
  • 1
    Dear @Diamond, first a commendation for starting it with C. Second a recommendation. Use a good book for learning standard C circa 2022. Recently I do recommend gustedt.gitlabpages.inria.fr/modern-c . Commented Jan 30, 2022 at 9:57

2 Answers 2

1
#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;
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.