UBC104 Embedded Systems Functions & Pointers
Introduction to C - Arrays, Functions & Pointers <return-type> <function-name> (<param-list>) { <expr> } Example: int fname (int number, void* ptr) { return 0; Introduction to C - Arrays, Functions & Pointers
Introduction to C - Arrays, Functions & Pointers Example Program I 1 void hello(int a, int b) { 2 3 } 4 5 int main(int argc, char** argv) { 6 7 hello(1,2); 8 } (gdb) disass hello Dump of assembler code for function hello: 0x08048354 <hello+0>: push %ebp 0x08048355 <hello+1>: mov %esp,%ebp 0x08048357 <hello+3>: pop %ebp 0x08048358 <hello+4>: ret Introduction to C - Arrays, Functions & Pointers
Introduction to C - Arrays, Functions & Pointers Example Program II (gdb) disass main Dump of assembler code for function main: 0x08048359 <main+0>: push %ebp 0x0804835a <main+1>: mov %esp,%ebp 0x0804835c <main+3>: sub $0x8,%esp 0x0804835f <main+6>: and $0xfffffff0,%esp 0x08048362 <main+9>: mov $0x0,%eax 0x08048367 <main+14>: sub %eax,%esp 0x08048369 <main+16>: movl $0x2,0x4(%esp) 0x08048371 <main+24>: movl $0x1,(%esp) 0x08048378 <main+31>: call 0x8048354 <hello> 0x0804837d <main+36>: leave 0x0804837e <main+37>: ret Put parameter onto stack in reverse order and call address of function Introduction to C - Arrays, Functions & Pointers
Introduction to C - Arrays, Functions & Pointers Example Program III 1 void hello(int *a, int *b) { 2 3 } 4 5 int main(int argc, char** argv) { 6 int i, j; 7 8 i= 1; j= 2; 9 hello(&i, &j); 10 } Introduction to C - Arrays, Functions & Pointers
Introduction to C - Arrays, Functions & Pointers Parameters Pass by value Values of parameters are passed on the stack Pass by reference Addresses of parameter are passed on the stack hello(1, 2); i= 1; j= 2; hello(&i,&j); Introduction to C - Arrays, Functions & Pointers
Introduction to C - Arrays, Functions & Pointers Parameters Pass by value Values of parameters are passed on the stack Pass by reference Addresses of parameter are passed on the stack 0x08048369 <main+16>: movl $0x2,0x4(%esp) 0x08048371 <main+24>: movl $0x1,(%esp) 0x08048378 <main+31>: call 0x8048354 <hello> 0x08048377 <main+30>: lea 0xfffffff8(%ebp),%eax 0x0804837a <main+33>: mov %eax,0x4(%esp) 0x0804837e <main+37>: lea 0xfffffffc(%ebp),%eax 0x08048381 <main+40>: mov %eax,(%esp) 0x08048384 <main+43>: call 0x8048354 <hello> Introduction to C - Arrays, Functions & Pointers
Variable Parameter-List <return-type> <function-name>( <param-list>,…) { <expr> } Example: int foobar(int number, …) { } foobar(3,“Foo”, “Bar”, ”!”); Introduction to C - Arrays, Functions & Pointers
Variable argumentlist - Functions #include <stdarg.h> void va_start(va_list ap, last) : initializes the argument list ap type va_arg(va_list ap, type) : returns the next argument in the list void va_end(va_list ap) : terminates the use of the argument list ap Introduction to C - Arrays, Functions & Pointers
Example: Variable Parameter-List void foobar(int a, ...) { va_list ap; char *s; va_start(ap, a); s = va_arg(ap, char *); printf("Parameter 1: %s\n", s); va_end(ap); } 0x08048369 <main+16>: movl $0x80484a4,0xc(%esp) 0x08048371 <main+24>: movl $0x80484a6,0x8(%esp) 0x08048379 <main+32>: movl $0x80484aa,0x4(%esp) 0x08048381 <main+40>: movl $0x3,(%esp) 0x08048388 <main+47>: call 0x8048354 <foobar> Introduction to C - Arrays, Functions & Pointers
Introduction to C - Arrays, Functions & Pointers Pointers to Functions <return-type> (*<function-name>) (<param-list>) Declaration: int (*foobar_ptr) (int number, void *ptr); typedef int (*mainprt) (int argc, char** arg); Example: foobar_ptr= foobar; foobar_ptr(); Introduction to C - Arrays, Functions & Pointers
Introduction to C - Arrays, Functions & Pointers Pointer-to-Function void hello(int a, int b) { } int main(int argc, char** argv) { void (*func)(int, int); func= hello; func(1,2); Introduction to C - Arrays, Functions & Pointers
Call of Pointer-to-Function (gdb) disass main Dump of assembler code for function main: 0x08048359 <main+0>: push %ebp 0x0804835a <main+1>: mov %esp,%ebp 0x0804835c <main+3>: sub $0x18,%esp 0x0804835f <main+6>: and $0xfffffff0,%esp 0x08048362 <main+9>: mov $0x0,%eax 0x08048367 <main+14>: sub %eax,%esp 0x08048369 <main+16>: movl $0x8048354,0xfffffffc(%ebp) 0x08048370 <main+23>: movl $0x2,0x4(%esp) 0x08048378 <main+31>: movl $0x1,(%esp) 0x0804837f <main+38>: mov 0xfffffffc(%ebp),%eax 0x08048382 <main+41>: call *%eax 0x08048384 <main+43>: leave 0x08048385 <main+44>: ret Introduction to C - Arrays, Functions & Pointers
Example: Pointers-to-Functions struct flist { void (*printstring) (char *s); void (*printnumber) (int n); } typedef struct flist flist; void printer_pstring(char *s) { flist f1, f2; f1.printstring= printer_pstring; f2.printstring= screen_pstring; Introduction to C - Arrays, Functions & Pointers
Summary: Functions & Pointers Pointers contain Addresses! Addresses can reference data OR functions Calls to functions are jumps to addresses It does not matter where this address comes from Introduction to C - Arrays, Functions & Pointers
Introduction to C - Arrays, Functions & Pointers Recommended Reading “A tutorial on pointers and arrays in C” by Ted Jensen http://www.netcom.com/~tjensen/ptr/cpoint.htm Introduction to C - Arrays, Functions & Pointers