0
void Printarr(int col, int **arr){
    for(int i = 0; i < col; i++){
        for(int j = 0; j < col; j++)
            printf("%d ", arr[i][j]);
        printf("\n");
    }
}

this is function for printing array.

int main(){
    int col = 0;
    scanf("%d", &col);
    int abc[col][col];
    for(int i = 0; i < col; i++){
        for(int j = 0; j < col; j++){
            abc[i][j]=0;
        }
    }

and I want to print array abc.

Printarr(col, abc);

the error is when it run this code Printarr(col, abc);

No matching function for call to 'Printarr' 
5
  • 2
    You can't. C++ does not work this way. This is not even valid C or C++, but relies on your compiler's custom extension to the C++ standard. If this was submitted as a homework assignment, most professors will fail it, for not being valid C++. Commented Feb 22, 2020 at 14:52
  • 2
    Does this answer your question? How to pass 2D array (matrix) in a function in C? Commented Feb 22, 2020 at 14:54
  • 2
    Note that a bona fide 2D array is an array of arrays, not an array of pointers, thus a corresponding function parameter must be a pointer to a (correct-length) array, not a pointer to a pointer. Commented Feb 22, 2020 at 15:00
  • 1
    @walnut - The question was tagged C++ when Sam commented. Sam's comment is correct in C++ - which has never supported VLAs. VLAs are also optional in C11. Commented Feb 22, 2020 at 15:02
  • @walnut - Given that a number of C++ compilers support VLAs as a non-standard extension, it is understandable that the OP tagged the code as C++. Passing a 2D array to a function expecting a pointer to pointer is not valid in either C or C++. Commented Feb 22, 2020 at 22:26

1 Answer 1

1

Your declaration should look like this:

#include <stdio.h>

void Printarr(int col, int arr[][col]){
    for(int i = 0; i < col; i++){
        for(int j = 0; j < col; j++)
            printf("%d ", arr[i][j]);
        printf("\n");
    }
}

int main(){
    int col = 10;
    int abc[col][col];
    for(int i = 0; i < col; i++){
        for(int j = 0; j < col; j++){
            abc[i][j]=0;
        }
    }
    Printarr(col,abc);
    return 0;
}

Your function argument is a pointer to a pointer but you only give it a normal pointer.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.