4

I have a segmentationfault at the line :

cout <<  b[0][0];

Could someone tell me what should I do to fix my code?

#include <iostream>
using namespace std;

int** gettab(int tab[][2]){
   return (int**)tab;
}

int main() {
   int a[4][2] = {{0, 0}, {1, 0}, {2, 0}, {2, 1}};
   int ** b = gettab(a);
   cout <<  b[0][0];
   return 0;
}

5 Answers 5

6

A 2-dimensional array is not the same thing as an array of pointers, which is how int** is interpreted. Change the return type of gettab.

int* gettab(int tab[][2]){
   return &tab[0][0];
}

int main() {
  int a[4][2] = {{0, 0}, {1, 0}, {2, 0}, {2, 1}};
  int* b = gettab(a);
  cout << b[0]; // b[row_index * num_cols + col_index]
  cout << b[1 * 2 + 0]; // the 1 from {1, 0}
}

Or:

int (*gettab(int tab[][2]))[2] {
  return tab;
}
// or:
template<class T> struct identity { typedef T type; };
identity<int(*)[2]>::type gettab(int tab[][2]) {
  return tab;
}

int main() {
  int a[4][2] = {{0, 0}, {1, 0}, {2, 0}, {2, 1}};
  int (*b)[2] = gettab(a);
  cout << b[0][0];
}
Sign up to request clarification or add additional context in comments.

7 Comments

this line : int (*)[2] gettab2(int tab[][2]){ doesn't compile. I have the error : main.cpp|14|error: expected unqualified-id before ‘)’ token| main.cpp|14|error: expected initializer before ‘gettab2’|
Should be int (* gettab(int tab[][2])) [2] I recommend not returning arrays though :)
@Roger The downvote wasn't from me, but your second solution looks strange to me and doesn't compile with my compiler (VS 2005), does this work for you?
My fault on the syntax, fixed and provided an easier to read alternative. (A typedef of int(*)[2] works too.)
The downvote was actually before I had the code sample in there, which I edited in within the first 5 minutes.
|
3

Being c++, rather than c, there are much better ways of handling arrays of all sorts, and passing them around.

2 Comments

another good link: parashift.com/c++-faq-lite/operator-overloading.html#faq-13.11 " Why shouldn't my Matrix class's interface look like an array-of-array?"
@danio but my matrix class's interface does look like that, oh no D:
2

The type of tab without square brackets is not actually int **. It is actually int (*)[2]. When you apply two [] operators to the resulting pointer, you end up dereferencing the first value in your array, 0, as a NULL pointer. Try this instead:

#include <iostream>
using namespace std;

typedef int (*foo)[2];

foo gettab(int tab[][2]){
   return tab;
}

int main() {
   int a[4][2] = {{0, 0}, {1, 0}, {2, 0}, {2, 1}};
   foo b = gettab(a);
   cout <<  b[0][0];
   return 0;
}

Comments

1

Your seg fault us because you pass in an "int*" effectively. A 2D array is not a double pointer ...

You are best off using a pointer that is "x*y" in size and addressing it without the 2 dimensions ... the code will end up the same anyway as the compiler will just generate the same code you would have to write more explicitly anyway :)

Comments

0

a 2 diminsional array isn't the same thing as an array of pointers. a 2 dimensional array is just a pointer to a hunk of memory that you have told the compiler to let you access as a 2 dimensional array

int* gettab(int tab[][2]) {   
   return (int*)tab;
}

int main() {   
   int a[4][2] = {{0, 0}, {1, 0}, {2, 0}, {2, 1}};   
   int* b = gettab(a);   
   cout <<  b[0 + 2*0];   
   return 0;
}

will do what you want. But I wonder if you really need to be trying to return a 2 dimensional array from a function in the first place. Perhaps a less made-up example if what you are trying to do would be helpful?

edit: fixed missing *2 in the calculation [0 + (sizeof(int)*2)*0]. edit again: well that was dumb. the multiplication of the column size of 2 by the size of an int is automatic in this case. sizeof(int) removed.

1 Comment

What exactly is sizeof(int) doing in b[0 + sizeof(int)*0]?

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.