Top Banner
C - Functions, Pointers Jinyang Li
31

C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Aug 18, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

C - Functions, Pointers

Jinyang Li

Page 2: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Lesson Plan

• Control Flow– goto

• Function & variable storage• Pointers

Page 3: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

C’s Control flow

• Same as Java• Conditional:

– if ... else if... else– switch

• Loops: while, do while, for– continue– break

Page 4: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

goto statements allow jump anywhere

for(...) {for(...) {

for(...) {goto error

}}

}

error:code handling error

while (cond) {...

}B:...

A:if (cond = false) goto B;...goto AB:...

Any control flow primitive can be expressed as a bunch of goto’s.

The only acceptable scenario for using goto

There’s no try/catch or try/except in CThere’s no goto in Java or core Python

Page 5: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Avoid goto’s whenever possible

Page 6: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Avoid goto’s whenever possible

Page 7: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

C is a procedural-language

• C program consists of functions– Also called procedures or subroutines

• Why breaking code into functions?– Readability and Reusability

• Keep functions short– General rules of thumb:

• Small enough to keep the code in your head• Fits on screen without scrolling

Page 8: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Variable and its storage: local variable

Local variable scope:– Within the function/block the local variable is declared– Local variables with the same name in different scopes are unrelated– Shadowing: Nested variable “hides” the outer variable in areas where

they are both in scope

int add(int a, int b) {

int r = a + b;return r;

}

r’s scope is in function add

Local variables aredefined inside a function

int add(int a, int b) {

int r = a;{

int r;r += b;

}return r;

}

What does add compute?

You can view function argumentsas local variables defined in function body

Page 9: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Variable and its storage: local variable scope

int find(int start, int end) {

for (int i = 0; i < a; i++) {if ((i % 3) == 0)

break;} return i;

}

$gcc –std=c99 test.c

Page 10: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Variable and its storage: local variable scope

int find(int start, int end) {

int r;for (int i = 0; i < a; i++) {

if ((i % 3) == 0) {r = i;break;

}} return r;

}

Correct?

Page 11: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Variable and its storage: local variable storageLocal variable storage:

– allocated upon function invocation– deallocated upon function return

void add(int a, int b, int result) {

int result = a + b;return;

}

int main(){

int result;add(1, 2, result);printf(“r=%d\n”, result);

}

Program output?

Page 12: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Variable and its storage: global variable

• Global variable scope: – Can be accessed from within any function– May be shadowed

• Global variable storage– Allocated upon program start, deallocated when entire program exits

#include <stdio.h>

int r = 0;int sum(int a, int b){

r = a + b;}int main() {

sum(1,2);

printf(“r=%d\n”, r);}

Global variables are defined outside any function

int r = 0;

Page 13: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Function invocation

void swap(int a, int b) {

int tmp = a;a = b;b = tmp;

}

int main() {

int x = 1;int y = 2;swap(x, y);

printf(“x: %d, y: %d”, x, y);}

Result x: ?, y: ?

C (and Java) passes arguments by value

Page 14: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Function invocation

void swap(int x, int y) {

int tmp = x;x = y;y = tmp;

}

int main() {

int x = 1;int y = 2;swap(x, y);

printf(“x: %d, y: %d”, x, y);}

Result x: 1, y: 2

C (and Java) passes arguments by value

? ?

Page 15: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Function invocation

main.x:main.y:

swap.x:swap.y:

swap.tmp:

void swap(int a, int b) {

int tmp = x;x = y;y = tmp;

}

int main() {

int x = 1;int y = 2;swap(x, y);

printf(“x: %d, y: %d”, x, y);}

Page 16: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Pointers

Pointer is a memory address

Page 17: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Pointer…

1a:

char a = 1;

0x100x11

0x12

0x13

0x140x15

0x16

0x170x18

0x19

0x1a

0x1b

0xff

...0x1c

Addresses should be 8-byte long, but for the same of simplicity, picture only shows one byte

Page 18: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Pointer…

1a:

2

char a = 1;int b = 2;

0x100x11

0x12

0x13

0x140x15

0x16

0x170x18

0x19

0x1a

0x1b

0xff

...0x1c

…b:

Page 19: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Pointer…

0x10

1

char a = 1;

a:

2

int b = 2;

b:

char *x;x = &a;

x:

0x100x11

0x12

0x13

0x140x15

...

0x1c

0x1d

0xff

...0x1e

& gives address of variable

Can be combined as:char *x = &a;

Same as: char* x;

what happens if I writechar x = &a;

orint *x = &a;

type mismatch!

Page 20: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Pointer…

0x10

1

char a = 1;

a:

2

int b = 2;

b:

char *x = &a;

x:

0x100x11

0x12

0x13

0x140x15

...

0x1c

0x1d

0xff

...0x1e

Size of pointer on a 64-bit machine?

8 bytes

printf(“x=%p\n”, x);

You can print the value of a pointer variable with %p

(leading zeros are not printed)0x10

Page 21: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Pointer

0x10

1

char a = 1;

a:

2

int b = 2;

b:

char *x = &a;

x:

0x100x11

0x12

0x13

0x140x15

...

0x1c

0x1b

0x22

...

int *y = &b;

0x11

... ...

y:

Page 22: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

1

Pointer

0x10

3

char a = 1;

a:

2

int b = 2;

b:

char *x = &a;

x:

0x100x11

0x12

0x13

0x140x15

...

0x1c

0x1b

0x22

...

int *y = &b;

0x11

... ...

y:

*x = 3;

* operator dereferences a pointer, not to be confused with the * in (char *) which is part of

typename

Value of variable a after this statement?

Page 23: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Pointer

??

1

char a = 1;

a:

2

int b = 2;

b:

char *x = &a;

x:

0x100x11

0x12

0x13

0x140x15

...

0x1c

0x1b

0x12

...

int *y = &b;

0x11

... ...

y:

*x = 3;

what if x is uninitialized?

Dereferencing an arbitrary address value may result in “Segmentation fault” or a

random memory write

Page 24: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Pointer

0x0

1

char a = 1;

a:

2

int b = 2;

b:

char *x = NULL;

x:

0x100x11

0x12

0x13

0x140x15

...

0x1c

0x1b

0x22

...

int *y = &b;

0x11

... ...

y:

*x = 3;

Always initialize pointers!

Dereferencing NULL pointer definitely results in

“Segmentation fault”

Page 25: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Pointer

0x0

1

char a = 1;

a:

2

int b = 2;

b:

char *x = NULL;

x:

0x100x11

0x12

0x13

0x140x15

...

0x1c

0x1b

0x22

...

int *y = &b;

0x11

... ...

y:

*x = 3;

Page 26: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Pointer

0x10

3

char a = 1;

a:

127

int b = 2;

b:

char *x = &a;

x:

0x100x11

0x12

0x13

0x140x15

...

0x1c

0x1b

0x22

...

int *y = &b;

0x11

... ...

y:

*x = 3;*y = 127;

Page 27: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Double Pointer

0x10

3

char a = 1;

a:

127

int b = 2;

b:

char *x = &a;

x:

0x100x11

0x12

0x13

0x140x15

...

0x1c

0x22

...

int *y = &b;

??

...

xx:

*x = 3;*y = 127;

char **xx = &x;

Same as:char **xx;

xx = &x;

char **xx is the same as char** xx;

what if I writechar* xx;xx = &x;

0x15

printf(“xx=%p *xx=%p **xx=%d\n”, xx, *xx, **xx);

Page 28: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Double Pointer

0x11

3

char a = 1;

a:

127

int b = 2;

b:

char *x = &a;

y:

0x100x11

0x12

0x13

0x14

0x1b...

0x22

0x22

...

int *y = &b;

... ...

yy:

*x = 3;*y = 127;

char **xx = &x;int **yy = &y;

0x1b

printf(“yy=%p *yy=%p **yy=%d\n”, yy, *yy, **yy);

Page 29: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Common confusions on *

* has two meanings!!1. part of a pointer type name, e.g. char *, char **, int *2. the deference operator.

char a = 1;char *p = &a;*p = 2;

char *b, *c;

C’s syntax for declaring multiple pointer variables on

one linechar* b, c; does not work

char *f=p, *g=p;C’s syntax for declaring and initializing multiple pointer

variables on one line

char **d,**e;

char **m=&p, **n=&p;

Page 30: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

void swap(int* x, int* y) {

int tmp = *x;*x = *y;*y = tmp;

}int main() {

int x = 1;int y = 2;swap(&x, &y);

printf(“x:%d, y:%d”,x,y);}

Size and value of x, y, tmpin swap upon function entrance?

1

2main.y:...0xf0

0xf3

...0xf4

0xf7

main.x:

??

swap.y:

swap.x:

??

swap.tmp:

??

Pass pointers to function

Page 31: C-Functions,Pointers, Arrays · C program consists of functions (aka subroutines, procedures) Why breaking code into functions? –Readability –Reusability

Summary

• Control Flow– goto

• Local vs. global variable– Local variables allocated/deallocated upon function entrance/return– Global variable: always there

• Pointers– Pointer values are memory addresses– p =&x; (makes p points to variable x)– *p … (refers to the variable pointed to by p)