lecture 6: c data types: pointers · c data types: pointers diba mirza university of california,...

17
CSE 30: Computer Organization and Systems Programming Lecture 6: C Data types: Pointers Diba Mirza University of California, San Diego 1

Upload: others

Post on 09-Jul-2020

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

CSE 30: Computer Organization and Systems Programming

Lecture 6: C Data types: Pointers

Diba MirzaUniversity of California, San Diego

1

Page 2: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

C Integer Data Types

2

Type Size

int 4 bytes

short 2 bytes

long 4 bytes

long long 8 bytes

unsigned 4 bytes

Page 3: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

Other datatypes

3

char c=‘a’; /* 1 byte */

• By default, may be signed or unsigned

• Always enforce using the appropriate keyword

float f; /* 4 bytes */

double d; /* 8 bytes */

Page 4: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

Accessing value, Lvalue and Rvalue

int x,y; /* What is the value of x?*/

y=10;

x=20;

y= x>y ? x : y;

4

Page 5: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

Pointers• Pointer: A variable that contains the address of a variable

• Declaration: type * pointer_name;

5

int *x;

How do we initialize a pointer?

Why pointers?

Page 6: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

Accessing location

To access the location/address, use the address operator ‘&’

int *x;

int y=20;

6

20

102

y

Page 7: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

Pointers• How to make a pointer point to something.

7

int *x;

int y;

x y

y = 3;

x = &y;

102 120

Page 8: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

Pointers• How to make a pointer point to something?

8

int *x, y;

x ? y ?

y = 3;

x ? y 3

x = &y;

x 120 y 3

102 120

102 120

102 120

x points to ysizeof(x)=

Page 9: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

Pointer Diagrams

• Short hand diagram for the following scenario

9

x 120 y 3

102 120

Page 10: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

To change the value of a variable using pointers:

use dereference * operator to left of pointer name

10

x y 3

int y=3, *x;

x= &y;

*x = 5;

x y 3

102 120

Page 11: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

x y 3

Two ways of changing the value of any variable

Why this is useful will be clear when we discuss functions and pointers

11

Change the value of y directly:

Change the value of y indirectly (via pointer x):

Page 12: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

Pointers and Pointees

Q: Which of the following pointer diagrams best represents the outcome of the above code?

12

int *p1, *p2, x;p1 = &x;p2 = p1;

A.

x

B.

x

C. Neither, the code is incorrect

Page 13: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

Pointer and Pointee data typesQ: This code gives a warning at compile time. Why?

13

A. The pointer ‘p’ is made to point to a variable of incompatible type

B. *p does not contain a valid value because y was not initialized

char *p;int y;p = &y;

Page 14: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

Q: What happens when we run the following code?

14

C. Compile time error

D. Runtime error

int *p;*p = 5;

A.p 5 B.

p

5

Page 15: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

Segmentation faults (aka segfault)

• Indicates that your program has crashed!

• What caused the crash?

– Segfaults occur if your program is trying to read or write an illegal memory location.

15

Page 16: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

Q: What is the output of this code?

16

A. The value pointed to by p, which is 5

B. The value pointed to by p plus one, which is 6

C. Undefined

D. Compiler error

E. Segmentation fault

int *p, x = 5;p = &x;printf(“%d”,(*p)++);

Page 17: Lecture 6: C Data types: Pointers · C Data types: Pointers Diba Mirza University of California, San Diego 1. C Integer Data Types 2 Type Size int 4 bytes short 2 bytes long 4 bytes

Two important facts about Pointers

17

1) A pointer can only point to one type –(basic or derived ) such

as int, char, a struct, another pointer, etc

2) After declaring a pointer: int *ptr;

ptr doesn’t actually point to anything yet. We can either:

make it point to something that already exists, or

allocate room in memory for something new that it will

point to

Null check before dereferencing