GDB Dot Operator Dreferencing Pointer?

Viewed 184

When debugging with GDB, I noticed something very strange. Namely, the . operator, when used in a print statement, can act just like the -> operator dereferencing a single or even double pointer to get to a field of a struct.

Here's a simple example:

#include <stdio.h>

typedef struct node {
    struct node *next;
    int data;
} Node;

int main()
{
    Node head, *head_p, **head_pp;

    head.next = 0UL;
    head.data = 42;

    head_p = &head;
    head_pp = &head_p;

    // **GDB PAUSED HERE**
    printf("head.data: %d\n", head.data);
    
    // These won't compile because the . operator is misused
    //
    // printf("head_p.data: %d\n", head_p.data);
    // printf("head_pp.data: %d\n", head_pp.data);
         
}

Here's when GDB prints when paused at the printf:

GDB output

How can the . operator (in a GDB print statement) dereference a pointer like that??

Thanks!

1 Answers

The syntax of GDB's commands is not C. Expressions are evaluated on the module's language, in your case C/C++.

The documentation says:

., ->
Structure member, and pointer-to-structure member. For convenience, GDB regards the two as equivalent, choosing whether to dereference a pointer based on the stored type information. Defined on struct and union data.

Related