Daveid Fred
Daveid Fred

Reputation: 419

Finding address of a local variable in C with GDB

Say I have some C code which goes along the lines of:

void fun_1(unsigned int *age)

[...]

int main() {

    unsigned int age[24];
}

In GDB, how can I find the address of age?

Upvotes: 10

Views: 18463

Answers (2)

P.P
P.P

Reputation: 121347

Both ages are not the same in case if you are not aware. One is local in main and another is local to fun_1(). So unless you pass the address of age in main to fun_1() they are not going to have the same address. Just set a break point in main and see the address of age.

(gdb) break main
(gdb) p &age
.....
(gdb) break fun_1
(gdb) p &age
.....

Upvotes: 7

Andrejs Cainikovs
Andrejs Cainikovs

Reputation: 28424

Finding address is as simple as:

p &age

Upvotes: 15

Related Questions