Graphics Programs Reference
In-Depth Information
An additional unary operator called the dereference operator exists for use
with pointers. This operator will return the data found in the address the
pointer is pointing to, instead of the address itself. It takes the form of an
asterisk in front of the variable name, similar to the declaration of a pointer.
Once again, the dereference operator exists both in GDB and in C. Used in
GDB, it can retrieve the integer value int_ptr points to.
(gdb) print *int_ptr
$5 = 5
A few additions to the addressof.c code (shown in addressof2.c) will
demonstrate all of these concepts. The added printf() functions use format
parameters, which I'll explain in the next section. For now, just focus on the
program's output.
addressof2.c
#include <stdio.h>
int main() {
int int_var = 5;
int *int_ptr;
int_ptr = &int_var; // Put the address of int_var into int_ptr.
printf("int_ptr = 0x%08x\n", int_ptr);
printf("&int_ptr = 0x%08x\n", &int_ptr);
printf("*int_ptr = 0x%08x\n\n", *int_ptr);
printf("int_var is located at 0x%08x and contains %d\n", &int_var, int_var);
printf("int_ptr is located at 0x%08x, contains 0x%08x, and points to %d\n\n",
&int_ptr, int_ptr, *int_ptr);
}
The results of compiling and executing addressof2.c are as follows.
reader@hacking:~/booksrc $ gcc addressof2.c
reader@hacking:~/booksrc $ ./a.out
int_ptr = 0xbffff834
&int_ptr = 0xbffff830
*int_ptr = 0x00000005
int_var is located at 0xbffff834 and contains 5
int_ptr is located at 0xbffff830, contains 0xbffff834, and points to 5
r eader@hacking:~/booksrc $
When the unary operators are used with pointers, the address-of oper-
ator can be thought of as moving backward, while the dereference operator
moves forward in the direction the pointer is pointing.
Search WWH ::




Custom Search