23

I am declaring a two dimensional array as such:

char arr[10][10];
arr[0][0] = 'X';

Now I print in debugger;

(lldb) po arr[0][0]
'X'

Awesome!! No problem.

Now I am declaring a two dimensional array as such:

int col = 10;
int row = 10;
char arr[row][col];
arr[0][0] = 'X';

Now I print in debugger;

(lldb) po arr[0][0]
error: subscript of pointer to incomplete type 'char []'
error: 1 errors parsing expression

Why??

4
  • 1
    I suppose that neither DWARF nor gdb support variable length arrays. Commented Jan 22, 2016 at 16:51
  • 2
    double array as such: means? Commented Jan 22, 2016 at 16:52
  • 3
    @SouravGhosh "A two-dimensional array like this:" was my interpretation. Quite confusing since double makes you think of floats. Commented Jan 22, 2016 at 16:53
  • 2
    Note that the debugger has no clue on the dimension of the array, so you have to manually cast and dereference it. The debugger will works as you expect if you declare both row and col as const. ` (gdb) print arr[0][0] $2 = 88 'X' ` Commented Jan 22, 2016 at 17:06

3 Answers 3

18

The debugger doesn't know exactly how big the array is, so you need to apply a cast:

(gdb) p ((char (*)[10])arr)[0][0]
$2 = 88 'X'
Sign up to request clarification or add additional context in comments.

4 Comments

Had it been arr[10][3] would it need to be p ((char (*)[3])arr)[0][0]?
@sodiumnitrate Correct. The cast needs to match the type.
Thanks! Could you explain why it is not ((char (*)[10]arr)[0][0]?
@sodiumnitrate Because the sizes don't match. Each element of arr is an int [3]. The cast you give would say that each element is instead a int [10], so the sizes and offsets would be wrong.
15

So dbush is right. But here's why in a little more depth.

char arr[10][10];

isn't the same thing as

char arr[row][col];

Even though they look and act similar.

In the C89 standard, the second would be illegal as the compiler has no idea how much space to allocate for the variable (even though that's defined in the previous two lines).

Enter the C99 standard and they introduced something called variable length arrays. The space allocation for the array is determined at runtime rather then at compile-time. Now you can pass in a couple of variables to a function and declare an array with a size based on those variables. Yay!

But it means the compiler officially doesn't know details about the array. Like how big it is. And LLDB uses the Clang/LLVM compiler to make sense of the code.

It's an example of the language becoming slightly higher level and abstracting the work it does under the hood. And occasionally that bites you in the ass.

Comments

2

make the col and row const

const int col = 10; const int row = 10;

1 Comment

This is a C language question. In C making them const will not make a difference. In C language const on col and row will not prevent the array from becoming VLA and thus will have no effect of the original issue.

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.