3

Possible Duplicate:
2D arrays with C++

Hi, I'm trying to copy a pointer to a matrix that i'm passing in to a function in C++. here's what my code is trying to express

#include <iostream>
using namespace std;

void func( char** p  )
{
    char** copy = p;
    cout << **copy;
}

int main()
{   
    char x[5][5];
    x[0][0] = 'H';
    func( (char**) &x);
    return 0;
}

However, this gives me a Seg Fault. Would someone please explain (preferrably in some detail) what underlying mechanism i'm missing out on? (and the fix for it)

Thanks much in advance :)

Community
  • 1
  • 1
David T
  • 31
  • 1
  • 3
  • 4
    A `char**` is **not** the same thing as a `char [][]`. Really. There are many questions in which this is discussed at length. I'll poke around a bit and find one or two. – dmckee --- ex-moderator kitten Mar 31 '11 at 21:04
  • 2
    http://stackoverflow.com/q/1285457/2509 is c++ specific, and http://stackoverflow.com/q/917783/2509 http://stackoverflow.com/q/2003745/2509 http://stackoverflow.com/q/2003745/2509 are all tagged [c], but as far as using arrays and pointers go the issues are the same (note that there are more choices in c++). – dmckee --- ex-moderator kitten Mar 31 '11 at 21:07

3 Answers3

1

char** is a pointer to a pointer (or an array of pointers). &x is not one of those - it's a pointer to a two-dimensional array of chars, which can be implicitly converted to a pointer to a single char (char *). The compiler probably gave you an error, at which point you put in the cast, but the compiler was trying to tell you something important.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Actually, `&x` is a pointer to the entire 2D array. Its type is `char(*)[5][5]`. – fredoverflow Mar 31 '11 at 21:20
  • @FredOverflow, I knew there was something amiss with my description but I didn't know the best way to word it - you helped me figure it out, thanks. – Mark Ransom Mar 31 '11 at 21:38
1

A pointer to an array of 5 arrays of 5 char (char x[5][5]) has the type "pointer to array of 5 arrays of 5 chars", that is char(*p)[5][5]. The type char** has nothing to do with this.

#include <iostream>
using namespace std;

void func( char (*p)[5][5]  )
{
    char (*copy)[5][5] = p;
    cout << (*copy)[0][0];
}

int main()
{
    char x[5][5];
    x[0][0] = 'H';
    func(&x);
    return 0;
}

Of course there are many other ways to pass a 2D array by reference or pointer, as already mentioned in comments. The most in-detail reference is probably StackOverflow's own C++ FAQ, How do I use arrays in C++?

Community
  • 1
  • 1
Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • Your way works, but only for the first element. Try setting x[1][1] to something like '-' in your main() and then retrieving it in "void func()", it won't work. i'll try to figure exactly why, guess i actually have to read up on this. thanks for suggesting – David T Apr 01 '11 at 03:38
  • @David T: it works for every element: https://ideone.com/hAITL – Cubbi Apr 01 '11 at 03:59
0

Try this instead of using a char**:

#include <iostream>
using namespace std;

void func( char* &p  )
{
    char* copy = p;
    cout << copy[0];
}

int main()
{   
    char x[5][5];
    x[0][0] = 'H';
    func(&x[0]);
    return 0;
}
Nick Banks
  • 4,298
  • 5
  • 39
  • 65