Download presentation
1
UBC104 Embedded Systems Functions & Pointers
2
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
3
Introduction to C - Arrays, Functions & Pointers
Example Program I void hello(int a, int b) { 2 } 4 int main(int argc, char** argv) { 6 hello(1,2); } (gdb) disass hello Dump of assembler code for function hello: 0x <hello+0>: push %ebp 0x <hello+1>: mov %esp,%ebp 0x <hello+3>: pop %ebp 0x <hello+4>: ret Introduction to C - Arrays, Functions & Pointers
4
Introduction to C - Arrays, Functions & Pointers
Example Program II (gdb) disass main Dump of assembler code for function main: 0x <main+0>: push %ebp 0x a <main+1>: mov %esp,%ebp 0x c <main+3>: sub $0x8,%esp 0x f <main+6>: and $0xfffffff0,%esp 0x <main+9>: mov $0x0,%eax 0x <main+14>: sub %eax,%esp 0x <main+16>: movl $0x2,0x4(%esp) 0x <main+24>: movl $0x1,(%esp) 0x <main+31>: call 0x <hello> 0x d <main+36>: leave 0x e <main+37>: ret Put parameter onto stack in reverse order and call address of function Introduction to C - Arrays, Functions & Pointers
5
Introduction to C - Arrays, Functions & Pointers
Example Program III void hello(int *a, int *b) { 2 } 4 int main(int argc, char** argv) { 6 int i, j; 7 8 i= 1; j= 2; hello(&i, &j); } Introduction to C - Arrays, Functions & Pointers
6
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
7
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 0x <main+16>: movl $0x2,0x4(%esp) 0x <main+24>: movl $0x1,(%esp) 0x <main+31>: call 0x <hello> 0x <main+30>: lea 0xfffffff8(%ebp),%eax 0x a <main+33>: mov %eax,0x4(%esp) 0x e <main+37>: lea 0xfffffffc(%ebp),%eax 0x <main+40>: mov %eax,(%esp) 0x <main+43>: call 0x <hello> Introduction to C - Arrays, Functions & Pointers
8
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
9
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
10
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); } 0x <main+16>: movl $0x80484a4,0xc(%esp) 0x <main+24>: movl $0x80484a6,0x8(%esp) 0x <main+32>: movl $0x80484aa,0x4(%esp) 0x <main+40>: movl $0x3,(%esp) 0x <main+47>: call 0x <foobar> Introduction to C - Arrays, Functions & Pointers
11
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
12
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
13
Call of Pointer-to-Function
(gdb) disass main Dump of assembler code for function main: 0x <main+0>: push %ebp 0x a <main+1>: mov %esp,%ebp 0x c <main+3>: sub $0x18,%esp 0x f <main+6>: and $0xfffffff0,%esp 0x <main+9>: mov $0x0,%eax 0x <main+14>: sub %eax,%esp 0x <main+16>: movl $0x ,0xfffffffc(%ebp) 0x <main+23>: movl $0x2,0x4(%esp) 0x <main+31>: movl $0x1,(%esp) 0x f <main+38>: mov 0xfffffffc(%ebp),%eax 0x <main+41>: call *%eax 0x <main+43>: leave 0x <main+44>: ret Introduction to C - Arrays, Functions & Pointers
14
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
15
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
16
Introduction to C - Arrays, Functions & Pointers
Recommended Reading “A tutorial on pointers and arrays in C” by Ted Jensen Introduction to C - Arrays, Functions & Pointers
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.