Re: Printing a 2D array in a C program

Jan Kratochvil
2016-03-04T14:42:37Z

On Fri, 04 Mar 2016 15:27:14 +0100, Neven Sajko wrote:
> In my C program there is a square matrix implemented as an array
> of arrays, which prints nicely in GDB with `info locals`.

I do not understand how that can happen.
You should always provide sample code / example.

> But how can that nice output be accomplished if you are not in
> the function in which the array was declared, but just have it
> passed as a function parameter.

If it is a pointer (as you say C, not C++ reference) you should be able to:
(gdb) print *thatpointername
Then GDB should IMO print it the same as a local variable is printed.

Printing of C++ vectors of vectors (which is BTW wrong data structure for a 2D
matrix anyway) as a matrix was implemented by Chris Moller as a Python Pretty
printer in some versions of Fedora GDB but it was later discontinued. The
initial implementation was:
http://pkgs.fedoraproject.org/cgit/rpms/gdb.git/commit/?id=6068e6305ed7d05b4a919c28aa5bcb737e1f163b
(gdb) p test2
$2 =
{
{0 1 }
{2 3 }
{4 5 }
}

Jan