Graphics Programs Reference
In-Depth Information
This is rather hacky, but since this integer value is typecast into the
proper pointer types when it is assigned and dereferenced, the end result is
the same. Notice that instead of typecasting multiple times to do pointer
arithmetic on an unsigned integer (which isn't even a pointer), the sizeof()
function is used to achieve the same result using normal arithmetic.
reader@hacking:~/booksrc $ gcc pointer_types5.c
reader@hacking:~/booksrc $ ./a.out
[hacky_nonpointer] points to 0xbffff810, which contains the char 'a'
[hacky_nonpointer] points to 0xbffff811, which contains the char 'b'
[hacky_nonpointer] points to 0xbffff812, which contains the char 'c'
[hacky_nonpointer] points to 0xbffff813, which contains the char 'd'
[hacky_nonpointer] points to 0xbffff814, which contains the char 'e'
[hacky_nonpointer] points to 0xbffff7f0, which contains the integer 1
[hacky_nonpointer] points to 0xbffff7f4, which contains the integer 2
[hacky_nonpointer] points to 0xbffff7f8, which contains the integer 3
[hacky_nonpointer] points to 0xbffff7fc, which contains the integer 4
[hacky_nonpointer] points to 0xbffff800, which contains the integer 5
r eader@hacking:~/booksrc $
The important thing to remember about variables in C is that the com-
piler is the only thing that cares about a variable's type. In the end, after the
program has been compiled, the variables are nothing more than memory
addresses. This means that variables of one type can easily be coerced into
behaving like another type by telling the compiler to typecast them into the
desired type.
0x266
Command-Line Arguments
Many nongraphical programs receive input in the form of command-line
arguments. Unlike inputting with scanf() , command-line arguments don't
require user interaction after the program has begun execution. This tends
to be more efficient and is a useful input method.
In C, command-line arguments can be accessed in the main() function by
including two additional arguments to the function: an integer and a pointer
to an array of strings. The integer will contain the number of arguments, and
the array of strings will contain each of those arguments. The commandline.c
program and its execution should explain things.
commandline.c
#include <stdio.h>
int main(int arg_count, char *arg_list[]) {
int i;
printf("There were %d arguments provided:\n", arg_count);
for(i=0; i < arg_count; i++)
printf("argument #%d\t-\t%s\n", i, arg_list[i]);
}
Search WWH ::




Custom Search