Miscellaneous functions Command Execution The function system() int system(const char *s); The string s contains a system command system(“pwd”); system(“ls >fname”); system(“prog”); Return value depends on system command Learn that from man page
Miscellaneous functions Command Execution Example: int a, b; char command[MAXCMD]; sprintf(command, "prog %d %d > prog.out", a, b); system(command); In program compiled as prog, get a and b values through argc and argv[ ]. System call doesn't return data from the command, but by writing > prog.out creates an output file The calling program can then input this file
Miscellaneous Functions Storage Management - malloc()/free() Review: Why is this incorrect? for (p=head; p!=NULL; p=p->next) free(p); Review: Why must it be written like this? for (p = head; p != NULL; p = q){ q = p->next; }
Miscellaneous Functions Math functions Need to use #include <math.h> in source code And include the math library flag for gcc: gcc source.c –lm *Note: the –lm has to be at the end of the line
Pointers to Functions Function prototype with pointer to function void qsort ( … , int (*comp) (void *, void *)); Function call passing a pointer to function qsort( …, (int (*) (void *, void *))strcmp); This is a cast to function pointer of strcmp Within qsort(), function is called via a pointer if ((*comp) (v[i], v[left]) < 0) …
Pointers to Functions Initialize a pointer to a function /* function pointer *fooptr = cast of foo to func ptr */ int (*fooptr) (int *, int*) = (int (*) (int *, int *)) foo; Call the function foo via the pointer to it (*fooptr) (to, from);
Miscellaneous Functions Random number generator functions:<stdlib.h> rand(void) Produces a random number between 0 and RAND_MAX (at least 32767 on any implementation) srand(unsigned int seed) Sets the state or "seed" of the random number generator. When two random number generators are initialized with the same seed, they produce the same sequence of random numbers.
qsort and binsearch Library function qsort prototype: void qsort(void *base, size_t n, size_t size, int (*cmp) (const void *, const void *)); It sorts an array of data using quick sort algorithm base a pointer to the table (an array of ??) n the number of elements in the table size the size of each element What’s the last argument? A pointer to a compare function for specific data type
qsort Notice: qsort doesn’t understand the data type for the elements of the table it sorts How can we tell that? base (the pointer to the table) is a type void * size (the size of each element) is provided (if qsort knew the data type, code could use “sizeof”) Last arg is a pointer to correct compare function It doesn’t know anything about what it sorts
qsort Example /* a compare function for integers, we will pass a pointer to this function to qsort */ int intcompare(int *i, int *j){ return (*i - *j); }
qsort Example main (){ int i; int a[10] = {8, 2, 9, 6, 5, 1, 3, 7, 4, 0}; qsort (a, 10, sizeof(int), (int (*) (void *, void *)) intcompare); for (i = 0; i < 10; i++) printf(“%d, ”, a[i]); } /* prints “0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ” */
bsearch Library function bsearch prototype: void *bsearch(const void *key, const void *base, size_t n, size_t size, int (*cmp) (const void *, const void *)); It searches for element containing key in an array of data that is already sorted in ascending order Returns: pointer to element (if found) or NULL (if not found) Last argument is a pointer to compare function
Variable Length Argument Lists Both printf and scanf have an argument (the format string) that defines the number and type of the remaining arguments in the list C does not support multiple declarations of the same function each with different lists How is it supported in C? Look at stack frame after a function call!
Typical Stack frame Stack Pointer After Call Decreasing Addresses Before Call A r g 1 A r g 2 A r g 3 Function’s Automatic Variables Return Data e.g.PC, other Registers, etc Code provides the location of the last fixed argument in call sequence to va_start Address 0xffffffff From fixed arguments, the code must determine the number of additional arguments to access via offsets from stack pointer and va_arg can work its way back up the stack to get each argument
Variable Length Argument Lists Use va_list data type and va_ macro package inside function with a variable length argument list to get args va_start, va_arg, va_end macros are defined in /usr/include/stdarg.h void foo (int n, ... ){ // Ellipsis is actual code! va_list ap; /* variable name ap */ va_start(ap, n); /* n is last named arg */ Now ap points just before first unnamed arg
Variable Length Argument Lists Each call to va_arg( ) moves pointer ap by one argument and returns value by type: ival = va_arg(ap, int); fval = va_arg(ap, float); sval = va_arg(ap, char *); Function must clean up before returning: va_end(ap); }