0

This question already has answers here: Trying passing as a paramater two dimesional array In C (3 answers) Your post has been associated with a similar question. If this question doesn’t resolve your question, ask a new one.

Closed 38 mins ago by rsjaffe, klutt c . (Private feedback for you)

rsjaffe and klutt, there is no answer what I am looking for, I am asking about this in low-level programming principally and the answers there are not practical.

I have encountered a problem with passing two dimensional arrays to other function as parameter. It was not working when I tried as below.

#include <stdio.h>

int display(int **src) {
    printf("%d", src[0][1]);
}

int main() {
    int arr[2][2] = {{1,2}, {3,4}};
    display(arr);
    return 0;
}

It raises segmentation fault error. So I changed the display function as below

int display(int src[][3]) {
    printf("%d", src[0][1]);
}

I am not sure why first case raises error. Please help me to understand deeply about this case.

Bojan
  • 637
  • 7
  • 17
  • 2
    Why the C++ tag? – Daniel Langr May 20 '20 at 06:56
  • 4
    If your question gets closed, then _don't_ copy/paste it and ask it again. Instead ask in comments why it was closed in case you don't understand why, then edit the original question if needed. Posting it again is frowned upon and might get you banned. – Lundin May 20 '20 at 06:56
  • Sorry, there is no language C/C++. There is C and there is C++ (which evolved out of C but became a language on its own). Your code does rather look like C but not like C++ (considering e.g. `#include `). – Scheff's Cat May 20 '20 at 06:57
  • Btw. `int **src` is no 2d array - it's just a pointer to a pointer to an `int`. `int (*src)[2]` might be a pointer to an arrray of `int` with 2 elements which decayed from (and may be used to access an) 2d array. – Scheff's Cat May 20 '20 at 06:59

0 Answers0