Re: Printing a 2D array in a C program

Neven Sajko <[email protected]>
Newsgroups gmane.comp.gdb.devel
Message-ID <CAL+bK4MV9ZLkon2PONYRpF0506P8zE3=Ukm0F1zpVDHd2HpAtw@mail.gmail.com>
On 4 March 2016 at 15:42, Jan Kratochvil <[email protected]> wrote:
> 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
 Thanks for your answer, Jan.

In my question I asked about C (not C++), I even mentioned the
gcc -std=gnu90 option.

My code is thus:

enum {
        sz = 17
};

void p(int m[sz][sz], int n) {
        int i;
        for (i=1; i<n; i++) {
                int j;
                for (j=i-1; 0<=j; j--) {
                        m[i][j] = abs(3*m[i-1][j] +2*m[i][j+1]) % 9340506;
                }
        }
}

void f(int n) {
        int m[sz][sz];

        g(m, n);

        p(m, n);

        r(m, n);
}


So, when I am in f, `info locals` prints it like {{a11, ...,
a1n}, ..., {an1, ..., ann}}.
But in p `print *m` just gets me {a11, ..., a1n}.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.