6

I have function like this:

void findScarf1(bool ** matrix, int m, int n, int radius, int connectivity); 

and in main function I create 2d dynamic array to pass in this function

    bool matrix[6][7] = {
    {0, 0, 1, 1, 1, 0, 0},
    {0, 0, 1, 1, 1, 0, 0},
    {0, 0, 1, 1, 1, 0, 0},
    {0, 0, 1, 1, 1, 0, 0},
    {0, 0, 1, 1, 1, 0, 0},
    {0, 0, 1, 1, 1, 0, 0}
};

The problem is:

findScarf1(matrix, 6, 7, 3, 4);

causes error C2664: 'findScarf1' : cannot convert parameter 1 from 'bool [6][7]' to 'bool **'

How to initialize array compactly(simultaneously with declaration)?

p.s. sorry if it's duplicate question but I've spent 1.5 hours figuring it out

5
  • 1
    Arrays are not pointers. You're passing a pointer (after decay) to an array where a pointer to a pointer is expected. Commented Jul 10, 2013 at 9:12
  • 2
    stackoverflow.com/a/8767247/2494803 Commented Jul 10, 2013 at 9:13
  • try: bool matrix[6][7], and bool matrix[][7], and bool (*matrix)[7] Commented Jul 10, 2013 at 9:14
  • possible duplicate of passing 2D array to function Commented Jul 10, 2013 at 9:15
  • Or just use std::array: std::array<std::array<bool, 6>, 7>. Also, this is not a dynamic array. Commented Jul 10, 2013 at 9:15

4 Answers 4

3

If you look at how your array is laid out in memory, and compare it how a pointer-to-pointer "matrix" is laid out, you will understand why you can't pass the matrix as a pointer to pointer.

You matrix is like this:

[ matrix[0][0] | matrix[0][1] | ... | matrix[0][6] | matrix[1][0] | matrix[1][1] | ... ]

A matrix in pointer-to-pointer is like this:

[ matrix[0] | matrix[1] | ... ]
  |           |
  |           v
  |           [ matrix[1][0] | matrix[1][1] | ... ]
  v
  [ matrix[0][0] | matrix[0][1] | ... ]

You can solve this by changing the function argument:

bool (*matrix)[7]

That makes the argument matrix a pointer to an array, which will work.


And by the way, the matrix variable you have is not dynamic, it's fully declared and initialized by the compiler, there's nothing dynamic about it.

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

Comments

2

Technically, a 2D array is an array of 1D arrays. So it cannot convert into pointer to pointer. It can convert into pointer to array, though.

So this should work:

void findScarf1(bool (*matrix)[7], int m, int n, int radius, int connectivity); 

Here bool (*matrix)[7] declares a pointer to array of 7 bool.

Hope that helps.

5 Comments

You have to add that in function findScaf1() he can access elements (*matrix)[i][j] and matrix[i][j] will be wrong, I am sure otherwise OP make mistake.
@GrijeshChauhan, How would it be wrong? That's what it decays to when indexing it normally.
@GrijeshChauhan: matrix[i][j] is absolutely correct syntax. In fact, (*matrix)[i][j] is WRONG!!!
@chris , Nawaz I was wrong, you both are correct :), but let me keep this comment undeleted may be helpsome one
@GrijeshChauhan: Yes, don't delete it. It is important for programmers to learn what not to do. :-)
1

You may define the function as:

void findScarf1(bool * matrix, int m, int n, int radius, int connectivity);

Or

void findScarf1(bool matrix[][7], int m, int n, int radius, int connectivity);

No matter how many dimensions an array has, it's just a block of linear memory.

When you use the first manner, you may need to do a cast when call this function:

findScarf1((bool *)marix, 6, 7, 3, 4);

Comments

1

My Following example may be helpful for you:

#include<stdio.h>
void f(int (*m)[7]){  // As Nawaz answred
 printf("%d\n", m[3][3]);

}
void _f(int m[6][7]){ // As I commented to your question
 printf("%d\n", m[3][3]);

}
void _f_(int m[][7]){// Second form of Nawaz's answe 
 printf("%d\n", m[3][3]);

}
void f_(int (*m)[6][7]){// Pointer of 2D array
 printf("%d\n", (*m)[3][3]);
}

int main(){

    int matrix[6][7] = {
    {0, 0, 1, 1, 1, 0, 0},
    {0, 0, 1, 3, 1, 0, 0},
    {0, 0, 1, 4, 1, 0, 0},
    {0, 0, 1, 5, 1, 0, 0},
    {0, 0, 1, 6, 1, 0, 0},
    {0, 0, 1, 7, 1, 0, 0}
    };
    f(matrix);
    _f(matrix);
    _f_(matrix);    
    f_(&matrix);
    return 1;
}    

question not tanged to c, but I compiled with gcc (I have not installed g++).

~$ gcc  -Wall -pedantic 2d.c
~$ ./a.out 
5
5
5
5

I was not intended to post an answer, but because I commented wrong to Nawaz answer so during an experiment I written this code.

Here one can find it working at codepacde

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.