Here is a simple program:
#include <stdio.h>
#include <string.h>
typedef struct {
char str[10];
} my_struct;
static my_struct s1;
int main()
{
char *temp_str = "test";
strcpy(s1.str, temp_str);
printf("%s\n", s1.str);
}
This program compiles and runs as expected, but I am seeing some odd behavior with gdb that I don't quite understand. These gdb commands were run following a breakpoint set on the printf line.
(gdb) p s1.str
$5 = "test\000\000\000\000\000"
(gdb) printf "%s\n", s1.str
test
(gdb) set $tmp = s1
(gdb) printf "%s\n", $tmp.str
Attempt to take address of value not located in memory.
Why does the last command not work? In more complex situations where accessing the variable directly (s1.str) isn't as clean, is there any valid way to use printf with something like $tmp in this example? This is with gdb 7.2.
The following does work however:
(gdb) set $tmp_str = s1.str
(gdb) printf "%s\n", $tmp_str
test
One other point of interest is that if I add an int to the struct (in this case, x - set to 4), I can successfully do:
(gdb) set $tmp = s1
(gdb) printf "%d\n", $tmp.x
4
printf "%s\n", $tmp.strandprintf "%s\n", $tmp->stroutputs "test"