Contents|Index|Previous|Next

Assignment to variables

To alter the value of a variable, evaluate an assignment expression. See Expressions. For example, print x=4 stores the value 4 into the variable, x, and then prints the value of the assignment expression (which is 4). See Using GDB with Different Languages for more information on operators in supported languages.

If you are not interested in seeing the value of the assignment, use the set command instead of the print command. set is really the same as print except that the expression’s value is not printed and is not put in the value history (see Value history). The expression is evaluated only for its effects.

If the beginning of the argument string of the set command appears identical to a set subcommand, use the set variable command instead of only set. This command is identical to set except for its lack of subcommands.

For example, if your program has a variable, width, you get an error if you try to set a new value with just ‘set width=13’, because GDB has the command set width:

(gdb) whatis width
type = double
(gdb) p width
$4 = 13
(gdb) set width=47
Invalid syntax in expression.

The invalid expression, of course, is ‘=47’. In order to actually set the program’s variable, width, use (gdb) set var width=47.

GDB allows more implicit conversions in assignments than C; you can freely store an integer value into a pointer variable or vice versa, and you can convert any structure to any other structure that is the same length or shorter. To store values into arbitrary places in memory, use the ‘{...}’ construct to generate a value of specified type at a specified address (see Expressions). For example, {int}0x83040 refers to memory location 0x83040 as an integer (which implies a certain size and representation in memory), and set {int}0x83040 = 4 stores the value 4 into that memory location.