Pointers II 1 Outline Pointers arithme.c and others - - PowerPoint PPT Presentation

pointers ii
SMART_READER_LITE
LIVE PREVIEW

Pointers II 1 Outline Pointers arithme.c and others - - PowerPoint PPT Presentation

Pointers II 1 Outline Pointers arithme.c and others Func.ons & pointers 2 Pointer Arithme/c When you add to or subtract from a pointer,


slide-1
SLIDE 1

Pointers ¡II ¡

1 ¡

slide-2
SLIDE 2

Outline ¡

  • Pointers ¡arithme.c ¡and ¡others ¡
  • Func.ons ¡& ¡pointers ¡

2 ¡

slide-3
SLIDE 3

Pointer ¡Arithme/c ¡

  • When ¡you ¡add ¡to ¡or ¡subtract ¡from ¡a ¡pointer, ¡the ¡amount ¡by ¡which ¡you ¡do ¡that ¡is ¡

mul/plied ¡by ¡the ¡size ¡of ¡the ¡type ¡the ¡pointer ¡points ¡to. ¡ ¡

  • In ¡the ¡case ¡of ¡our ¡three ¡increments, ¡each ¡1 ¡that ¡you ¡added ¡was ¡mul.plied ¡by ¡

sizeof(int). ¡ int ¡array[] ¡= ¡{ ¡45, ¡67, ¡89 ¡}; ¡ int ¡*array_ptr ¡= ¡array; ¡ ¡ prinH(" ¡first ¡element: ¡%i\n", ¡*(array_ptr++));1 ¡ prinH("second ¡element: ¡%i\n", ¡*(array_ptr++)); ¡ prinH(" ¡third ¡element: ¡%i\n", ¡*array_ptr); ¡

¡

Output: ¡ first ¡element: ¡45 ¡ ¡ second ¡element: ¡67 ¡ ¡ third ¡element: ¡89 ¡ NOTE ¡1: ¡1==4 ¡(programmer ¡ ¡humor?!) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡*(array_ptr++) ¡== ¡*array_ptr++ ¡ *(array_ptr++) ¡1 ¡ vs ¡ (*array_ptr)++ ¡ find ¡the ¡value ¡at ¡that ¡ address, ¡output, ¡then ¡add ¡ “1” ¡to ¡the ¡address ¡ ¡VS ¡ Find ¡the ¡value ¡at ¡the ¡ address, ¡output, ¡then ¡add ¡

  • ne ¡to ¡the ¡value ¡at ¡that ¡

address ¡ B ¡ T ¡ W

slide-4
SLIDE 4

Pointer ¡Arithme/c ¡(cont) ¡

Expression ¡ Assuming ¡p ¡is ¡a ¡ pointer ¡to ¡a… ¡ … ¡and ¡the ¡size ¡

  • f ¡*p ¡is… ¡

Value ¡added ¡ to ¡the ¡ ¡pointer ¡ p+1 ¡ char ¡ 1 ¡ 1 ¡ p+1 ¡ short ¡ 2 ¡ 2 ¡ p+1 ¡ int ¡ 4 ¡ 4 ¡ p+1 ¡ double ¡ 8 ¡ 8 ¡ p+2 ¡ char ¡ 1 ¡ 2 ¡ p+2 ¡ short ¡ 2 ¡ 4 ¡ p+2 ¡ int ¡ 4 ¡ 8 ¡ p+2 ¡ double ¡ 8 ¡ 16 ¡

4 ¡

slide-5
SLIDE 5

Pointer ¡Arithme/c ¡(again) ¡

  • pointer ¡(+ ¡or ¡-­‑) ¡integer ¡

– Only ¡for ¡pointers ¡that ¡are ¡poin.ng ¡at ¡an ¡element ¡of ¡an ¡ array ¡ – Also ¡works ¡with ¡malloc ¡ – Watch ¡for ¡bounds ¡(begin ¡and ¡end) ¡

  • Ok ¡to ¡go ¡one ¡beyond ¡the ¡array ¡but ¡not ¡a ¡valid ¡dereference ¡

¡

  • pointer#2 ¡– ¡pointer#1 ¡

– Only ¡allowed ¡when ¡both ¡point ¡to ¡elements ¡of ¡the ¡same ¡ array ¡and ¡p1 ¡index ¡< ¡p2 ¡index ¡ – Measured ¡in ¡array ¡elements ¡not ¡bytes ¡ – If ¡p1 ¡à ¡array[i] ¡and ¡p2 ¡à ¡array[j] ¡then ¡p2-­‑p1 ¡== ¡j ¡-­‑ ¡i ¡

5 ¡

slide-6
SLIDE 6

Pointer ¡Indexing ¡

6 ¡

int ¡array[] ¡= ¡{ ¡45, ¡67, ¡89 ¡}; ¡ int ¡*array_ptr ¡= ¡&array[1]; ¡ ¡ prinH("%i\n", ¡array_ptr[1]); ¡ //output ¡is ¡89 ¡(whoooooooaaaahhhhff??!!) ¡ int ¡array[] ¡= ¡{ ¡45, ¡67, ¡89 ¡}; ¡ prinH("%i\n", ¡array[0]); ¡ ¡ ¡ ¡// ¡output ¡is ¡45 ¡ prinH("%i\n", ¡*array); ¡ ¡ ¡ ¡// ¡output ¡is ¡45 ¡ // ¡*array ¡and ¡array[0] ¡mean ¡same ¡thing ¡

array ¡points ¡to ¡the ¡first ¡element ¡of ¡the ¡array; ¡ ¡ ¡

array[1] ¡== ¡*(array ¡+ ¡1) ¡

array_ptr ¡is ¡set ¡to ¡&array[1], ¡so ¡it ¡points ¡to ¡the ¡second ¡element ¡of ¡the ¡

  • array. ¡ ¡

So ¡array_ptr[1] ¡is ¡equivalent ¡to ¡array[2] ¡ ¡

slide-7
SLIDE 7

NULL ¡ ¡vs ¡ ¡0 ¡ ¡vs ¡ ¡‘\0’ ¡

  • NULL ¡is ¡a ¡macro ¡defined ¡in ¡several ¡standard ¡headers ¡
  • 0 ¡is ¡an ¡integer ¡constant ¡
  • '\0' ¡is ¡a ¡character ¡constant, ¡and ¡ ¡

– nul ¡is ¡the ¡name ¡of ¡the ¡character ¡constant. ¡ ¡ ¡

All ¡of ¡these ¡are ¡*not* ¡interchangeable ¡

  • NULL ¡is ¡to ¡be ¡used ¡for ¡pointers ¡only ¡since ¡it ¡may ¡be ¡defined ¡as ¡((void ¡*) ¡0), ¡this ¡

would ¡cause ¡problems ¡with ¡anything ¡but ¡pointers. ¡

  • 0 ¡can ¡be ¡used ¡anywhere, ¡it ¡is ¡the ¡generic ¡symbol ¡for ¡each ¡type's ¡zero ¡value ¡and ¡the ¡

compiler ¡will ¡sort ¡things ¡out. ¡

  • '\0' ¡should ¡be ¡used ¡only ¡in ¡a ¡character ¡context. ¡

– nul ¡is ¡not ¡defined ¡in ¡C ¡or ¡C++, ¡it ¡shouldn't ¡be ¡used ¡unless ¡you ¡define ¡it ¡yourself ¡in ¡a ¡suitable ¡ manner, ¡like: ¡

  • #define ¡nul ¡ ¡'\0' ¡

7 ¡

slide-8
SLIDE 8

R ¡and ¡L ¡values ¡

  • L-­‑value ¡= ¡something ¡that ¡can ¡appear ¡on ¡the ¡lee ¡side ¡of ¡an ¡equal ¡sign ¡ ¡

– A ¡place ¡i.e. ¡memory ¡loca.on ¡for ¡a ¡value ¡to ¡be ¡stored ¡

  • R-­‑value ¡is ¡something ¡that ¡can ¡appear ¡on ¡the ¡right ¡side ¡of ¡an ¡equal ¡sign ¡

– A ¡value ¡

  • Example: ¡ ¡

– a ¡= ¡b+25 ¡ ¡vs ¡b+25 ¡= ¡a ¡

  • Example: ¡ ¡

– int ¡a[30]; ¡ ¡ – a[b+10]=0; ¡

  • Example: ¡ ¡

– int ¡a, ¡*pi; ¡ ¡ – pi ¡= ¡&a; ¡ ¡ – *pi ¡= ¡20; ¡

8 ¡

slide-9
SLIDE 9

R ¡and ¡L ¡values ¡(cont) ¡

  • Given: ¡

– char ¡ ¡ch ¡= ¡‘a’; ¡ – char ¡*cp ¡= ¡&ch; ¡

¡ NOTE: ¡the ¡? ¡is ¡the ¡loca/on ¡ that ¡follows ¡ch ¡

9 ¡

Problem ¡ Expression ¡ R-­‑value ¡ L-­‑value ¡ 1 ¡ ch ¡ yes ¡ yes ¡ 2 ¡ &ch ¡ yes ¡ illegal ¡ 3 ¡ cp ¡ yes ¡ yes ¡ 4 ¡ &cp ¡ yes ¡ illegal ¡ 5 ¡ *cp ¡ yes ¡ yes ¡ 6 ¡ *c+1 ¡ yes ¡ illegal ¡ 7 ¡ *(c+1) ¡ yes ¡ yes ¡ 8 ¡ ++cp ¡ yes ¡ illegal ¡ 9 ¡ cp++ ¡ yes ¡ illegal ¡ 10 ¡ *++cp ¡ yes ¡ yes ¡ 11 ¡ *cp++ ¡ yes ¡ yes ¡ 12 ¡ ++*cp ¡ yes ¡ illegal ¡ 13 ¡ (*cp)++ ¡ yes ¡ illegal ¡ 14 ¡ ++*++cp ¡ yes ¡ illegal ¡ 15 ¡ ++*cp++ ¡ yes ¡ illegal ¡

cp ¡ ch ¡ a ¡ ? ¡

slide-10
SLIDE 10

An ¡Array ¡of ¡Character ¡Pointers ¡

10 ¡

#include<stdio.h> ¡ ¡ int ¡main() ¡ ¡ { ¡ ¡ ¡ ¡ ¡// ¡Declaring/Ini/alizing ¡3 ¡characters ¡pointers ¡ ¡ ¡ ¡ ¡ ¡ ¡char ¡*ptr1 ¡= ¡"Himanshu"; ¡ ¡ ¡ ¡ ¡ ¡ ¡char ¡*ptr2 ¡= ¡"Arora"; ¡ ¡ ¡ ¡ ¡ ¡ ¡char ¡*ptr3 ¡= ¡"TheGeekStuff"; ¡ ¡ ¡ ¡ ¡ ¡//Declaring ¡an ¡array ¡of ¡3 ¡char ¡pointers ¡ ¡ ¡ ¡ ¡ ¡ ¡char* ¡arr[3]; ¡ ¡ ¡ ¡ ¡ ¡// ¡Ini/alizing ¡the ¡array ¡with ¡values ¡ ¡ ¡ ¡ ¡ ¡ ¡arr[0] ¡= ¡ptr1; ¡ ¡ ¡ ¡ ¡ ¡ ¡arr[1] ¡= ¡ptr2; ¡ ¡ ¡ ¡ ¡ ¡ ¡arr[2] ¡= ¡ptr3; ¡ ¡ ¡ ¡ ¡ ¡//Prin/ng ¡the ¡values ¡stored ¡in ¡array ¡ ¡ ¡ ¡ ¡ ¡ ¡prinH("\n ¡[%s]\n", ¡arr[0]); ¡ ¡ ¡ ¡ ¡ ¡ ¡prinH("\n ¡[%s]\n", ¡arr[1]); ¡ ¡ ¡ ¡ ¡ ¡ ¡prinH("\n ¡[%s]\n", ¡arr[2]); ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡0; ¡ ¡ } ¡

slide-11
SLIDE 11

Pointers ¡to ¡Arrays ¡

  • <data ¡type> ¡(*<name ¡of ¡ptr>)[<an ¡integer>] ¡

– Declares ¡a ¡pointer ¡ptr ¡to ¡an ¡array ¡of ¡5 ¡integers. ¡

  • int(*ptr)[5]; ¡

11 ¡

#include<stdio.h> ¡ ¡ int ¡main(void) ¡ ¡ { ¡ ¡ ¡ ¡ ¡char ¡arr[3]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡char ¡(*ptr)[3]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡arr[0] ¡= ¡'a'; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡arr[1] ¡= ¡'b'; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡arr[2] ¡= ¡'c'; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ptr ¡= ¡&arr; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡0; ¡ ¡ } ¡

Declares ¡and ¡ini/alizes ¡an ¡array ¡‘arr’ ¡and ¡then ¡ declares ¡a ¡pointer ¡‘ptr’ ¡to ¡an ¡array ¡of ¡3 ¡

  • characters. ¡Then ¡ini/alizes ¡ptr ¡with ¡the ¡address ¡
  • f ¡array ¡‘arr’. ¡

int ¡*arr[8]; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡An ¡array ¡of ¡int ¡pointers. ¡ int ¡(*arr)[8]; ¡ ¡ ¡ ¡ ¡// ¡A ¡pointer ¡to ¡an ¡array ¡of ¡integers ¡

slide-12
SLIDE 12

Outline ¡

  • Pointers ¡arithme.c ¡and ¡others ¡
  • Func.ons ¡& ¡pointers ¡

12 ¡

slide-13
SLIDE 13

Func/on ¡defini/on ¡

  • Func.on ¡prototype ¡

– Return ¡type* ¡ – Argument ¡defini.on ¡ – return_type ¡ ¡func.on_name ¡(type1 ¡arg1,type2 ¡arg2,..,typen ¡argn) ¡

  • Func.on ¡calls ¡

– Basic ¡syntax ¡ – Parameter ¡passing* ¡

  • Standard ¡library ¡and ¡func.on ¡calls ¡

NOTES: Ø Functions should be short and sweet. Ø Functions do not nest. Ø Variables have to be communicated through function arguments or global variables. Ø * If no return type or no argument type, then it defaults to int

Sample: hop://www.cprogrammingexpert.com/images/Func.on.gif ¡

int ¡add(int ¡p,int ¡q) ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡p+q; ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ z ¡= ¡add(a,b); ¡ int ¡add(int ¡p,int ¡q); ¡

13 ¡

slide-14
SLIDE 14

Func/on ¡Prototypes ¡

  • A ¡number ¡of ¡statements ¡grouped ¡into ¡a ¡single ¡logical ¡unit ¡are ¡called ¡a ¡

func.on ¡

  • REMINDER ¡è ¡It ¡is ¡necessary ¡to ¡have ¡a ¡single ¡func.on ¡‘main’ ¡in ¡every ¡C ¡
  • program. ¡
  • A ¡func.on ¡prototype ¡is ¡a ¡func.on ¡declara.on ¡or ¡defini.on ¡which ¡includes: ¡

– Informa.on ¡about ¡the ¡number ¡ ¡of ¡arguments ¡ – Informa.on ¡about ¡the ¡types ¡of ¡the ¡arguments ¡

  • Although ¡you ¡are ¡allowed ¡not ¡to ¡specify ¡any ¡informa.on ¡about ¡a ¡

func.on's ¡arguments ¡in ¡a ¡declara.on, ¡it ¡is ¡purely ¡because ¡of ¡backwards ¡ compa.bility ¡with ¡Old ¡C ¡and ¡should ¡be ¡avoided ¡(poor ¡coding ¡style). ¡

– A ¡declara.on ¡without ¡any ¡informa.on ¡about ¡the ¡arguments ¡is ¡not ¡a ¡

  • prototype. ¡

Only ¡one ¡func.on ¡with ¡a ¡given ¡name ¡may ¡be ¡defined. ¡Unlike ¡C++, ¡C ¡does ¡not ¡ support ¡overloading ¡(i.e., ¡two ¡func.ons ¡with ¡the ¡same ¡name ¡but ¡different ¡ signatures). ¡

14 ¡

slide-15
SLIDE 15

The ¡RETURN ¡statement ¡

  • Every ¡func.on ¡except ¡those ¡returning ¡void ¡should ¡have ¡

at ¡least ¡one, ¡each ¡return ¡showing ¡what ¡value ¡is ¡ supposed ¡to ¡be ¡returned ¡at ¡that ¡point. ¡ ¡

  • Although ¡it ¡is ¡possible ¡to ¡return ¡from ¡a ¡func.on ¡by ¡

falling ¡through ¡the ¡last ¡}, ¡unless ¡the ¡func.on ¡returns ¡ void, ¡an ¡unknown ¡value ¡will ¡be ¡returned, ¡resul.ng ¡in ¡ undefined ¡behavior. ¡

  • The ¡type ¡of ¡expression ¡returned ¡must ¡match ¡the ¡type ¡
  • f ¡the ¡func.on, ¡or ¡be ¡capable ¡of ¡being ¡converted ¡to ¡it ¡

as ¡if ¡an ¡assignment ¡statement ¡were ¡in ¡use. ¡

  • Following ¡the ¡return ¡keyword ¡with ¡an ¡expression ¡is ¡not ¡

permioed ¡if ¡the ¡func.on ¡returns ¡void. ¡

15 ¡

slide-16
SLIDE 16

Pointers ¡and ¡Func/ons ¡ ¡

  • Pass ¡By ¡Value ¡

– Passing ¡a ¡variable ¡by ¡value ¡makes ¡a ¡copy ¡of ¡the ¡variable ¡before ¡passing ¡ it ¡onto ¡a ¡func.on. ¡This ¡means ¡that ¡if ¡you ¡try ¡to ¡modify ¡the ¡value ¡inside ¡ a ¡func.on, ¡it ¡will ¡only ¡have ¡the ¡modified ¡value ¡inside ¡that ¡func.on. ¡ Once ¡the ¡func.on ¡returns, ¡the ¡variable ¡you ¡passed ¡it ¡will ¡have ¡the ¡ same ¡value ¡it ¡had ¡before ¡you ¡passed ¡it ¡into ¡the ¡func.on. ¡ ¡

  • Pass ¡By ¡Reference ¡ ¡

– There ¡are ¡two ¡instances ¡where ¡a ¡variable ¡is ¡passed ¡by ¡reference: ¡

  • When ¡you ¡modify ¡the ¡value ¡of ¡the ¡passed ¡variable ¡locally ¡(inside ¡the ¡callee) ¡ ¡

and ¡the ¡value ¡of ¡the ¡variable ¡in ¡the ¡calling ¡func.on ¡as ¡well. ¡

  • To ¡avoid ¡making ¡a ¡copy ¡of ¡the ¡variable ¡for ¡efficiency ¡reasons. ¡

– Technically, ¡C ¡does ¡not ¡“pass ¡by ¡reference” ¡as ¡typically ¡seen ¡in ¡other ¡ programming ¡languages. ¡Actually, ¡when ¡you ¡pass ¡a ¡pointer ¡(an ¡ address) ¡, ¡a ¡copy ¡is ¡made ¡of ¡that ¡variable ¡so ¡two ¡variables ¡point ¡to ¡the ¡ same ¡address ¡(one ¡from ¡the ¡callee ¡and ¡one ¡from ¡the ¡caller). ¡

16 ¡

slide-17
SLIDE 17

Pointers ¡and ¡Func/on ¡arguments ¡

  • Since ¡C ¡passes ¡arguments ¡to ¡func.ons ¡by ¡value ¡and ¡make ¡a ¡copy ¡local ¡to ¡swap; ¡so ¡ ¡there ¡is ¡no ¡direct ¡way ¡for ¡the ¡

called ¡func.on ¡(callee) ¡to ¡alter ¡a ¡variable ¡in ¡the ¡calling ¡func.on ¡(caller). ¡ ¡

  • Because ¡of ¡call ¡by ¡value, ¡swap ¡can’t ¡affect ¡the ¡arguments ¡a ¡and ¡b ¡in ¡the ¡rou.ne ¡that ¡called ¡it. ¡ ¡
  • The ¡way ¡to ¡obtain ¡the ¡desired ¡effect ¡is ¡for ¡the ¡calling ¡program ¡to ¡pass ¡pointers ¡to ¡the ¡values ¡to ¡be ¡changed: ¡
  • Since ¡the ¡operator ¡& ¡produces ¡the ¡address ¡of ¡a ¡variable, ¡&a ¡is ¡a ¡pointer ¡to ¡a. ¡In ¡swap ¡itself, ¡the ¡parameters ¡are ¡

declared ¡as ¡pointers, ¡and ¡the ¡operands ¡are ¡accessed ¡indirectly ¡through ¡them. ¡

  • NOW ¡KNOW ¡WHY ¡SCANF ¡NEEDS ¡& ¡SYMBOLS!!! ¡

/* ¡WRONG ¡*/ ¡ void ¡swap(int ¡x, ¡int ¡y) ¡ ¡ { ¡ ¡ ¡ ¡ ¡int ¡temp; ¡ ¡ ¡ ¡ ¡ ¡temp ¡= ¡x; ¡ ¡ ¡ ¡ ¡x ¡= ¡y; ¡ ¡ ¡ ¡ ¡y ¡= ¡temp; ¡ } ¡ /* ¡interchange ¡*px ¡and ¡*py ¡*/ ¡ void ¡swap(int ¡*px, ¡int ¡*py) ¡ { ¡ ¡ ¡ ¡ ¡int ¡temp; ¡ ¡ ¡ ¡ ¡temp ¡= ¡*px; ¡ ¡ ¡ ¡ ¡*px ¡= ¡*py; ¡ ¡ ¡ ¡ ¡*py ¡= ¡temp; ¡ } ¡

Call: ¡swap(a,b); ¡ Call: ¡swap(&a,&b); ¡

17 ¡

slide-18
SLIDE 18

Func/on ¡example ¡

#include ¡<stdio.h> ¡ #include ¡<stdlib.h> ¡ ¡ void ¡prinfotal(int ¡total); ¡ void ¡addxy(int ¡x, ¡int ¡y, ¡int ¡total); ¡ void ¡subxy(int ¡x, ¡int ¡y, ¡int ¡*total); ¡ ¡ void ¡main() ¡{ ¡ ¡ ¡ ¡ ¡int ¡x, ¡y, ¡total; ¡ ¡ ¡ ¡ ¡x ¡= ¡10; ¡ ¡ ¡ ¡ ¡y ¡= ¡5; ¡ ¡ ¡ ¡ ¡total ¡= ¡0; ¡ ¡ ¡ ¡ ¡prinfotal(total); ¡ ¡ ¡ ¡ ¡addxy(x, ¡y, ¡total); ¡ ¡ ¡ ¡ ¡prinfotal(total); ¡ ¡ ¡ ¡ ¡subxy(x, ¡y, ¡&total); ¡ ¡ ¡ ¡ ¡prinfotal(total); ¡} ¡ void ¡prinfotal(int ¡total) ¡{ ¡ ¡ ¡ ¡ ¡ ¡prinH("Total ¡in ¡Main: ¡%dn", ¡total); ¡ } ¡ ¡ void ¡addxy(int ¡x, ¡int ¡y, ¡int ¡total) ¡{ ¡ ¡ ¡ ¡ ¡ ¡total ¡= ¡x ¡+ ¡y; ¡ ¡ ¡ ¡ ¡ ¡prinH("Total ¡from ¡inside ¡addxy: ¡%dn", ¡ total); ¡ ¡ ¡} ¡ ¡ void ¡subxy(int ¡x, ¡int ¡y, ¡int ¡*total) ¡{ ¡ ¡ ¡ ¡ ¡*total ¡= ¡x ¡-­‑ ¡y; ¡ ¡ ¡ ¡ ¡prinH("Total ¡from ¡inside ¡subxy: ¡%dn", ¡ *total); ¡ } ¡ Program ¡con/nued… ¡

18 ¡

slide-19
SLIDE 19

A ¡Func/on ¡example ¡

#include ¡<stdio.h> ¡ ¡ #include ¡<stdlib.h> ¡ ¡ ¡ void ¡date(int ¡*, ¡int ¡*); ¡ ¡ ¡ ¡ ¡ ¡/* ¡declare ¡the ¡func/on ¡*/ ¡ ¡ ¡ main() ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡month, ¡day; ¡ ¡ ¡ ¡ ¡ ¡ ¡date ¡(&day, ¡&month); ¡ ¡ ¡ ¡ ¡ ¡ ¡prinH("day ¡is ¡%d, ¡month ¡is ¡%d\n", ¡day, ¡month); ¡ ¡ ¡ ¡ ¡ ¡ ¡return ¡0;} ¡ ¡ ¡ void ¡date(int ¡*day_p, ¡int ¡*month_p) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡int ¡day_ret, ¡month_ret; ¡ ¡ ¡/* ¡* ¡At ¡this ¡point, ¡calculate ¡the ¡day ¡and ¡month ¡* ¡ ¡ ¡values ¡in ¡ ¡day_ret ¡and ¡month_ret ¡respec/vely. ¡*/ ¡ ¡ ¡ ¡ ¡ ¡ ¡*day_p ¡= ¡day_ret; ¡ ¡ ¡ ¡ ¡ ¡ ¡*month_p ¡= ¡month_ret; ¡} ¡

19 ¡

slide-20
SLIDE 20

Func/on ¡Summary ¡

  • Func.ons ¡can ¡be ¡called ¡recursively. ¡ ¡
  • Func.ons ¡can ¡return ¡any ¡type ¡that ¡you ¡can ¡declare, ¡except ¡for ¡arrays ¡and ¡

func.ons ¡(you ¡can ¡get ¡around ¡that ¡restric.on ¡to ¡some ¡extent ¡by ¡using ¡ pointers). ¡ ¡

  • Func.ons ¡returning ¡no ¡value ¡should ¡return ¡void. ¡ ¡
  • Always ¡use ¡func.on ¡prototypes. ¡ ¡
  • Undefined ¡behavior ¡results ¡if ¡you ¡call ¡or ¡define ¡a ¡func.on ¡anywhere ¡in ¡a ¡

program ¡unless ¡either ¡ ¡

– a ¡prototype ¡is ¡always ¡in ¡scope ¡for ¡every ¡call ¡or ¡defini.on, ¡or ¡ ¡ – you ¡are ¡very, ¡very ¡careful. ¡

  • Assuming ¡that ¡you ¡are ¡using ¡prototypes, ¡the ¡values ¡of ¡the ¡arguments ¡to ¡a ¡

func.on ¡call ¡are ¡converted ¡to ¡the ¡types ¡of ¡the ¡formal ¡parameters ¡exactly ¡ as ¡if ¡they ¡had ¡been ¡assigned ¡using ¡the ¡= ¡operator. ¡ ¡

  • Func.ons ¡taking ¡no ¡arguments ¡should ¡have ¡a ¡prototype ¡with ¡(void) ¡as ¡the ¡

argument ¡specifica.on. ¡

20 ¡

slide-21
SLIDE 21

Func/on ¡pointers ¡

21 ¡

  • Func.on ¡codes ¡are ¡also ¡stored ¡in ¡the ¡memory ¡
  • Func.on ¡pointers ¡are ¡pointers ¡poin.ng ¡to ¡the ¡start ¡address ¡of ¡

the ¡func.on ¡codes ¡

  • Func.on ¡pointers ¡can ¡be ¡passed ¡as ¡parameters ¡or ¡returned ¡as ¡

return ¡value ¡

Simple ¡example: ¡ #include ¡<stdio.h> ¡ ¡ void ¡my_int_func(int ¡x) ¡ ¡ { ¡prin}( ¡"%d\n", ¡x ¡); ¡} ¡ ¡ ¡ int ¡main() ¡{ ¡ ¡ ¡ ¡void ¡(*foo)(int); ¡ ¡ ¡ ¡/* ¡the ¡ampersand ¡is ¡actually ¡op.onal ¡*/ ¡ ¡ ¡ ¡foo ¡= ¡&my_int_func; ¡ ¡ ¡ ¡Int ¡a ¡= ¡1; ¡ ¡ ¡(*foo)(a); ¡ ¡ ¡return ¡0; ¡ ¡ } ¡

slide-22
SLIDE 22

Why ¡do ¡we ¡need ¡func/on ¡pointers? ¡ ¡

22 ¡

  • Run.me ¡binding—useful ¡when ¡alterna.ve ¡func.ons ¡maybe ¡

used ¡to ¡perform ¡similar ¡tasks ¡on ¡data ¡(eg ¡sor.ng) ¡ ¡

– ¡Determine ¡sor.ng ¡func.on ¡based ¡on ¡type ¡of ¡data ¡at ¡run ¡

.me ¡

  • ¡Eg: ¡inser.on ¡sort ¡for ¡smaller ¡data ¡sets ¡(n ¡<100) ¡
  • ¡Eg: ¡Quicksort ¡for ¡large ¡data ¡sets ¡( ¡n ¡> ¡100000) ¡
  • ¡Other ¡sor.ng ¡algorithms ¡based ¡on ¡type ¡of ¡data ¡set ¡
  • One ¡common ¡use ¡is ¡in ¡passing ¡a ¡func.on ¡as ¡a ¡parameter ¡in ¡a ¡

func.on ¡call ¡

  • Greater ¡flexibility ¡and ¡beoer ¡code ¡reuse ¡
slide-23
SLIDE 23

Func/on ¡Pointer ¡Declara/ons ¡

23 ¡

  • A ¡func.on ¡pointer ¡is ¡nothing ¡else ¡than ¡a ¡

variable, ¡it ¡must ¡be ¡defined ¡as ¡usual. ¡Eg, ¡ int ¡(*funcPointer) ¡(int, ¡char, ¡int); ¡

  • The ¡extra ¡parentheses ¡around ¡

(*funcPointer) ¡is ¡needed ¡because ¡there ¡are ¡ precedence ¡rela.onships ¡in ¡declara.on ¡just ¡ as ¡there ¡are ¡in ¡expressions ¡ ¡

slide-24
SLIDE 24

Func/on ¡Pointer ¡Assignment ¡

24 ¡

  • It ¡is ¡op.onal ¡to ¡use ¡the ¡address ¡operator ¡& ¡in ¡front ¡
  • f ¡the ¡func.on’s ¡name ¡
  • When ¡you ¡men.on ¡the ¡name ¡of ¡a ¡func.on ¡but ¡are ¡

not ¡calling ¡it, ¡there ¡is ¡nothing ¡else ¡you ¡could ¡possibly ¡ be ¡trying ¡to ¡do ¡except ¡for ¡genera.ng ¡a ¡pointer ¡to ¡it ¡

  • Similar ¡to ¡the ¡fact ¡that ¡a ¡pointer ¡to ¡the ¡first ¡element ¡
  • f ¡an ¡array ¡is ¡generated ¡automa.cally ¡when ¡an ¡array ¡

appears ¡in ¡an ¡expression ¡

slide-25
SLIDE 25

Func/on ¡Pointer ¡Assignment– ¡Example ¡

25 ¡

//Declare ¡a ¡func.on ¡pointer ¡ int ¡(*funcPointer) ¡(int, ¡char, ¡int); ¡ ¡ int ¡assignExample ¡( ¡int ¡a, ¡char ¡b, ¡int ¡c){ ¡ ¡ ¡ ¡prin}(“ ¡Welcome ¡to ¡the ¡assignment ¡example”); ¡ ¡ ¡ ¡return ¡a+b+c; ¡ } ¡ ¡ int ¡main(){ ¡ ¡ ¡funcPointer= ¡assignExample; ¡//assignment ¡ ¡ ¡funcPointer=&assignExample; ¡//alterna.ve ¡using ¡address ¡operator ¡ ¡ ¡… ¡ ¡ ¡return ¡0; ¡ } ¡

slide-26
SLIDE 26

Calling ¡a ¡func/on ¡by ¡Func/on ¡Pointer ¡

26 ¡

  • There ¡are ¡two ¡alterna.ves ¡

ü Use ¡the ¡name ¡of ¡the ¡func.on ¡pointer ¡ ü Can ¡explicitly ¡dereference ¡it ¡

int ¡(*funcPointer) ¡(int, ¡char, ¡int); ¡ // ¡calling ¡a ¡func.on ¡using ¡func.on ¡pointer ¡ ¡int ¡answer= ¡funcPointer ¡(7, ¡’A’ ¡, ¡2 ¡); ¡ ¡int ¡answer=(* ¡funcPointer) ¡(7, ¡’A’ ¡, ¡2 ¡); ¡

slide-27
SLIDE 27

Arrays ¡of ¡Func/on ¡Pointers ¡

27 ¡

  • C ¡treats ¡pointers ¡to ¡func.ons ¡just ¡like ¡pointers ¡to ¡data ¡

therefore ¡we ¡can ¡have ¡arrays ¡of ¡pointers ¡to ¡func.ons ¡

  • This ¡offers ¡the ¡possibility ¡to ¡select ¡a ¡func.on ¡using ¡an ¡

index ¡

ü Eg. ¡ ü ¡Suppose ¡that ¡we’re ¡wri.ng ¡a ¡program ¡that ¡displays ¡a ¡menu ¡of ¡

commands ¡for ¡the ¡user ¡to ¡choose ¡from. ¡We ¡can ¡write ¡ func.ons ¡that ¡implement ¡these ¡commands, ¡then ¡store ¡ pointers ¡to ¡the ¡func.ons ¡in ¡an ¡array: ¡

slide-28
SLIDE 28

Arrays ¡of ¡Func/on ¡Pointers– ¡Example ¡ ¡

28 ¡

void ¡(*file_cmd[]) ¡(void) ¡= ¡ ¡ { ¡ ¡new_cmd, ¡ ¡ ¡open_cmd, ¡ ¡ ¡close_cmd, ¡ ¡ ¡save_cmd ¡, ¡ ¡ ¡save_as_cmd, ¡ ¡ ¡print_cmd, ¡ ¡ ¡exit_cmd ¡ }; ¡ ¡ If ¡the ¡user ¡selects ¡a ¡command ¡between ¡0 ¡and ¡6, ¡then ¡we ¡can ¡ subscript ¡the ¡file_cmd ¡array ¡to ¡find ¡out ¡which ¡func.on ¡to ¡call ¡ ¡ file_cmd[n](); ¡ ¡ ¡ ¡

¡ ¡ ¡

slide-29
SLIDE 29

Sort ¡Example ¡

29 ¡

  • In ¡<stdlib.h>, ¡we ¡have ¡a ¡sor.ng ¡func.on: ¡

void ¡qsort ¡( ¡void ¡*base ¡, ¡size_t ¡num ¡, ¡size_t ¡size ¡, ¡ ¡ int ¡(*comp_func) ¡(const ¡void ¡*, ¡const ¡void ¡*)) ¡

  • Consists ¡of ¡three ¡parts ¡

ü a ¡comparison ¡that ¡determines ¡the ¡ordering ¡of ¡any ¡pair ¡of ¡objects ¡ ü an ¡exchange ¡that ¡reverses ¡their ¡order ¡ ü A ¡sor.ng ¡algorithm ¡that ¡makes ¡comparisons ¡and ¡exchange ¡un.l ¡the ¡objects ¡are ¡

in ¡order. ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡<the ¡sor.ng ¡algorithm ¡is ¡independent ¡of ¡comparison ¡and ¡exchange ¡operator> ¡

  • qsort ¡will ¡sort ¡an ¡array ¡of ¡elements. ¡This ¡is ¡a ¡wild ¡func.on ¡that ¡

uses ¡a ¡pointer ¡to ¡another ¡func.on ¡that ¡performs ¡the ¡required ¡

  • comparisons. ¡ ¡
slide-30
SLIDE 30

Sort ¡Example ¡

30 ¡

  • In ¡<stdlib.h>, ¡we ¡have ¡a ¡sor.ng ¡func.on: ¡

void ¡qsort ¡( ¡void ¡*base ¡, ¡size_t ¡num ¡, ¡size_t ¡size ¡, ¡ ¡ int ¡(*comp_func) ¡(const ¡void ¡*, ¡const ¡void ¡*)) ¡

  • Some ¡explana.on ¡

ü void ¡* ¡base ¡is ¡a ¡pointer ¡to ¡the ¡array ¡to ¡be ¡sorted. ¡This ¡can ¡be ¡a ¡pointer ¡to ¡any ¡

data ¡type ¡ ¡ ¡

ü size_t ¡num ¡The ¡number ¡of ¡elements. ¡ ¡ ü size_t ¡size ¡The ¡element ¡size. ¡ ¡ ü int ¡(*comp_func)(const ¡void ¡*, ¡const ¡void ¡*))This ¡is ¡a ¡pointer ¡to ¡a ¡func.on. ¡ ¡

slide-31
SLIDE 31

Sort ¡Example ¡

31 ¡

  • qsort ¡thus ¡maintains ¡it's ¡data ¡type ¡independence ¡by ¡giving ¡the ¡

comparison ¡responsibility ¡to ¡the ¡user. ¡ ¡

  • The ¡compare ¡func.on ¡must ¡return ¡integer ¡values ¡according ¡to ¡

the ¡comparison ¡result: ¡ ¡

ü less ¡than ¡zero ¡: ¡if ¡first ¡value ¡is ¡less ¡than ¡the ¡second ¡value ¡ ¡ ü zero ¡: ¡if ¡first ¡value ¡is ¡equal ¡to ¡the ¡second ¡value ¡ ¡ ü greater ¡than ¡zero ¡: ¡if ¡first ¡value ¡is ¡greater ¡than ¡the ¡second ¡value ¡ ¡

  • Some ¡quite ¡complicated ¡data ¡structures ¡can ¡be ¡sorted ¡in ¡this ¡
  • manner. ¡ ¡
  • The ¡generic ¡pointer ¡type ¡void ¡* ¡is ¡used ¡for ¡the ¡pointer ¡

arguments, ¡any ¡pointer ¡can ¡be ¡cast ¡to ¡void ¡* ¡and ¡back ¡again ¡ without ¡loss ¡of ¡informa.on. ¡

slide-32
SLIDE 32

32 ¡

#include <stdlib.h> int int_sorter( const void *first_arg, const void *second_arg ){ int first = *(int*)first_arg; int second = *(int*)second_arg; if ( first < second ) { return -1; } else if ( first == second ) { return 0; } else { return 1; } } int main() { int array[10]; int i; /* fill array */ for ( i = 0; i < 10; ++i ) { array[ i ] = 10 - i; } qsort( array, 10 , sizeof( int ), int_sorter ); for ( i = 0; i < 10; ++i ) { printf ( "%d\n" ,array[ i ] ); } }

slide-33
SLIDE 33

References ¡

33 ¡

  • “The ¡C ¡Programming ¡Language”, ¡Brian ¡W.Kernighan, ¡

Dennis ¡M.Ritchie ¡

  • hop://www.cprogramming.com/tutorial/func.on-­‑

pointers.html ¡