35
1 Pointers Pointers ( ( םםםםםםם םםםםםםם) )

1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

  • View
    222

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

1

Pointers Pointers ((מצביעיםמצביעים))

Page 2: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

2

Variables in memory

Primitives Arrays

Page 3: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

3

Pointers

Pointer is a variable that contains the address of a variable

Here P is said to point to the variable C

C

7 3 4… …

173172 174 175 176 177 178 179 180 181

174 3 4… …

P

833832 834 835 836 837 838 839 840 841

Page 4: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

4

Brief Summary of today’s session…

&x – address (pointer) of variable x

*x – content in address x (common) usage: int x = *y; (common) usage: printf(“%d”,*y);

int */double */char * - define pointer to the corresponding primitive

(common) usage: int * x = &y; int ** ?

Page 5: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

5

Referencing

The unary operator & gives the address of a variable

The statement P=&C assigns the address of C to the

variable P, and now P points to C To print a pointer, use %p format.

Page 6: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

6

Referencing

int C;int *P; /* Declare P as a pointer to int

*/C = 7;P = &C;

C

7 3 4… …

173172 174 175 176 177 178 179 180 181

174 3 4… …

P

833832 834 835 836 837 838 839 840 841

Page 7: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

7

Dereferencing

The unary operator * is the dereferencing operator

Applied on pointers Access the object the pointer

points to The statement *P=5; Puts in C (the variable pointed by

P) the value 5

Page 8: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

8

Dereferencing

printf(“%d”, *P); /* Prints out ‘7’ */*P = 177;printf(“%d”, C); /* Prints out ‘177’ */P = 177; /* This is unadvisable! */

C

7 3 4… …

173172 174 175 176 177 178 179 180 181

174 3 4… …

P

833832 834 835 836 837 838 839 840 841

177

177

Page 9: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

9

Example

pointers.c

Page 10: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

10

pointers.c – step by step

int x=1, y=2, z[10]={5,6,7};int *ip; /* ip is a pointer to int */

ip = &x; /* ip now points to x */printf("ip now points to x that contains the value %d\n",*ip);y = *ip; /* y is now 1 */printf("y is now %d\n",y);*ip = 0; /* x is now 0 */printf("x is now %d\n",x);ip = &z[2]; /* ip now points to z[2] */printf("ip now points to z[2] that contains the value %d\n",*ip);*ip = 1; /* z[2] is now 1 */printf("z[2] is now %d\n", z[2]);printf("ip is %p\n", ip);

x y

z ip

1 2

364

5 6 7

Z[0] Z[1] Z[2]

120 248

364 368 372

564 772

Page 11: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

11

pointers.c – step by step

int x=1, y=2, z[10]={5,6,7};int *ip; /* ip is a pointer to int */

ip = &x; /* ip now points to x */printf("ip now points to x that contains the value %d\n",*ip);y = *ip; /* y is now 1 */printf("y is now %d\n",y);*ip = 0; /* x is now 0 */printf("x is now %d\n",x);ip = &z[2]; /* ip now points to z[2] */printf("ip now points to z[2] that contains the value %d\n",*ip);*ip = 1; /* z[2] is now 1 */printf("z[2] is now %d\n", z[2]);printf("ip is %p\n", ip);

x y

z ip

1 2

364

5 6 7

Z[0] Z[1] Z[2]

120 248

364 368 372

120

564 772

Page 12: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

12

pointers.c – step by step

int x=1, y=2, z[10]={5,6,7};int *ip; /* ip is a pointer to int */

ip = &x; /* ip now points to x */printf("ip now points to x that contains the value %d\n",*ip);y = *ip; /* y is now 1 */printf("y is now %d\n",y);*ip = 0; /* x is now 0 */printf("x is now %d\n",x);ip = &z[2]; /* ip now points to z[2] */printf("ip now points to z[2] that contains the value %d\n",*ip);*ip = 1; /* z[2] is now 1 */printf("z[2] is now %d\n", z[2]);printf("ip is %p\n", ip);

x y

z ip

1 2

364

5 6 7

Z[0] Z[1] Z[2]

120 248

364 368 372

120

564 772

Page 13: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

13

pointers.c – step by step

int x=1, y=2, z[10]={5,6,7};int *ip; /* ip is a pointer to int */

ip = &x; /* ip now points to x */printf("ip now points to x that contains the value %d\n",*ip);y = *ip; /* y is now 1 */printf("y is now %d\n",y);*ip = 0; /* x is now 0 */printf("x is now %d\n",x);ip = &z[2]; /* ip now points to z[2] */printf("ip now points to z[2] that contains the value %d\n",*ip);*ip = 1; /* z[2] is now 1 */printf("z[2] is now %d\n", z[2]);printf("ip is %p\n", ip);

x y

z ip

1 1

364

5 6 7

Z[0] Z[1] Z[2]

120 248

364 368 372

120

564 772

Page 14: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

14

pointers.c – step by step

int x=1, y=2, z[10]={5,6,7};int *ip; /* ip is a pointer to int */

ip = &x; /* ip now points to x */printf("ip now points to x that contains the value %d\n",*ip);y = *ip; /* y is now 1 */printf("y is now %d\n",y);*ip = 0; /* x is now 0 */printf("x is now %d\n",x);ip = &z[2]; /* ip now points to z[2] */printf("ip now points to z[2] that contains the value %d\n",*ip);*ip = 1; /* z[2] is now 1 */printf("z[2] is now %d\n", z[2]);printf("ip is %p\n", ip);

x y

z ip

1 1

364

5 6 7

Z[0] Z[1] Z[2]

120 248

364 368 372

120

564 772

Page 15: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

15

pointers.c – step by step

int x=1, y=2, z[10]={5,6,7};int *ip; /* ip is a pointer to int */

ip = &x; /* ip now points to x */printf("ip now points to x that contains the value %d\n",*ip);y = *ip; /* y is now 1 */printf("y is now %d\n",y);*ip = 0; /* x is now 0 */printf("x is now %d\n",x);ip = &z[2]; /* ip now points to z[2] */printf("ip now points to z[2] that contains the value %d\n",*ip);*ip = 1; /* z[2] is now 1 */printf("z[2] is now %d\n", z[2]);printf("ip is %p\n", ip);

x y

z ip

0 1

364

5 6 7

Z[0] Z[1] Z[2]

120 248

364 368 372

120

564 772

Page 16: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

16

pointers.c – step by step

int x=1, y=2, z[10]={5,6,7};int *ip; /* ip is a pointer to int */

ip = &x; /* ip now points to x */printf("ip now points to x that contains the value %d\n",*ip);y = *ip; /* y is now 1 */printf("y is now %d\n",y);*ip = 0; /* x is now 0 */printf("x is now %d\n",x);ip = &z[2]; /* ip now points to z[2] */printf("ip now points to z[2] that contains the value %d\n",*ip);*ip = 1; /* z[2] is now 1 */printf("z[2] is now %d\n", z[2]);printf("ip is %p\n", ip);

x y

z ip

0 1

364

5 6 7

Z[0] Z[1] Z[2]

120 248

364 368 372

120

564 772

Page 17: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

17

pointers.c – step by step

int x=1, y=2, z[10]={5,6,7};int *ip; /* ip is a pointer to int */

ip = &x; /* ip now points to x */printf("ip now points to x that contains the value %d\n",*ip);y = *ip; /* y is now 1 */printf("y is now %d\n",y);*ip = 0; /* x is now 0 */printf("x is now %d\n",x);ip = &z[2]; /* ip now points to z[2] */printf("ip now points to z[2] that contains the value %d\n",*ip);*ip = 1; /* z[2] is now 1 */printf("z[2] is now %d\n", z[2]);printf("ip is %p\n", ip);

x y

z ip

0 1

364

5 6 7

Z[0] Z[1] Z[2]

120 248

364 368 372

372

564 772

Page 18: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

18

pointers.c – step by step

int x=1, y=2, z[10]={5,6,7};int *ip; /* ip is a pointer to int */

ip = &x; /* ip now points to x */printf("ip now points to x that contains the value %d\n",*ip);y = *ip; /* y is now 1 */printf("y is now %d\n",y);*ip = 0; /* x is now 0 */printf("x is now %d\n",x);ip = &z[2]; /* ip now points to z[2] */printf("ip now points to z[2] that contains the value %d\n",*ip);*ip = 1; /* z[2] is now 1 */printf("z[2] is now %d\n", z[2]);printf("ip is %p\n", ip);

x y

z ip

0 1

364

5 6 7

Z[0] Z[1] Z[2]

120 248

364 368 372

372

564 772

Page 19: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

19

pointers.c – step by step

int x=1, y=2, z[10]={5,6,7};int *ip; /* ip is a pointer to int */

ip = &x; /* ip now points to x */printf("ip now points to x that contains the value %d\n",*ip);y = *ip; /* y is now 1 */printf("y is now %d\n",y);*ip = 0; /* x is now 0 */printf("x is now %d\n",x);ip = &z[2]; /* ip now points to z[2] */printf("ip now points to z[2] that contains the value %d\n",*ip);*ip = 1; /* z[2] is now 1 */printf("z[2] is now %d\n", z[2]);printf("ip is %p\n", ip);

x y

z ip

0 1

364

5 6 1

Z[0] Z[1] Z[2]

120 248

364 368 372

372

564 772

Page 20: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

20

pointers.c – step by step

int x=1, y=2, z[10]={5,6,7};int *ip; /* ip is a pointer to int */

ip = &x; /* ip now points to x */printf("ip now points to x that contains the value %d\n",*ip);y = *ip; /* y is now 1 */printf("y is now %d\n",y);*ip = 0; /* x is now 0 */printf("x is now %d\n",x);ip = &z[2]; /* ip now points to z[2] */printf("ip now points to z[2] that contains the value %d\n",*ip);*ip = 1; /* z[2] is now 1 */printf("z[2] is now %d\n", z[2]);printf("ip is %p\n", ip);

x y

z ip

0 1

364

5 6 1

Z[0] Z[1] Z[2]

120 248

364 368 372

372

564 772

Page 21: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

21

pointers.c – step by step

int x=1, y=2, z[10]={5,6,7};int *ip; /* ip is a pointer to int */

ip = &x; /* ip now points to x */printf("ip now points to x that contains the value %d\n",*ip);y = *ip; /* y is now 1 */printf("y is now %d\n",y);*ip = 0; /* x is now 0 */printf("x is now %d\n",x);ip = &z[2]; /* ip now points to z[2] */printf("ip now points to z[2] that contains the value %d\n",*ip);*ip = 1; /* z[2] is now 1 */printf("z[2] is now %d\n", z[2]);printf("ip is %p\n", ip);

x y

z ip

0 1

364

5 6 1

Z[0] Z[1] Z[2]

120 248

364 368 372

372

564 772

Page 22: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

22

Common errors It is impossible to define pointers to constants

or expressions. It is also impossible to change a variable’s

address (because it is not for us to determine!).

Therefore, the following are errors: i = &3; j = &(k+5); k = &(a==b); &a = &b; &a = 150;

Page 23: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

23

Pass arguments by value

The functions we saw till now accepted their arguments “by value”

They could manipulate the passed values

They couldn’t change values in the calling function

Page 24: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

24

Wrong swap (val_swap.c)

void swap(int x, int y) {int temp;

temp=x;x=y;y=temp;

}

int main(void) { int x=1,y=2;

printf("before swap: x=%d, y=%d\n",x,y);swap(x,y);printf("after swap: x=%d, y=%d\n",x,y);

}

Page 25: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

25

How can we fix it?

We can define swap so it gets pointers to integers instead of integers

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

…swap *x and *y… } We then call swap by swap(&x,&y); This is passing values by address

Page 26: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

26

Right Swap (add_swap.c)

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

temp=*x; *x=*y; *y=temp;}

int main(void) { int x=1,y=2;

printf("before swap: x=%d, y=%d\n",x,y); swap(&x,&y); printf("after swap: x=%d, y=%d\n",x,y);}

Page 27: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

27

Insights We can now understand the & in

scanf(“%d”,&a); The argument list in scanf is

simply passed by address, so scanf can change its content

Other relevant examples from the past?

Can we now “return” more then a single value from a function? How?

Page 28: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

28

Exercise

Write a function that accepts a double parameter and returns its integer and fraction parts.

Write a program that accepts a number from the user and prints out its integer and fraction parts, using this function

Page 29: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

29

Solution

dbl_split.c

Page 30: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

30

Exercise @ home

The relation between rectangular and polar coordinates is given by –

r = sqrt(x2+y2)θ = tan-1(y/x)

Implement a function that accepts two rectangular coordinates and returns the corresponding polar coordinates Use the function atan defined in math.h

Page 31: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

31

Solution

rec_to_polar.c

Page 32: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

32

Exercise

Implement a function that accepts an integer array, and finds the two numbers that are closest together

For example, if the array is –{1, 5, 7, 10, 6, 19}

The function should find 5 and 6 (or 6 and 7, it doesn’t matter)

Page 33: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

33

Solution

array.c

Page 34: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

34

Pointers and Arrays

Recall that an array S holds the address of its first element S[0]

S is actually a pointer to S[0] int S[10]; int *P; P=S; /* From now P is equivalent to S */

Both P and S are now pointing to S[0]

Page 35: 1 Pointers ( מצביעים ). 2 Variables in memory Primitives Arrays

35

Pointer-array equivalence Arrays are actually a kind of pointers! When an array is defined, a fixed

amount of memory the size of the array is allocated. The array variable is set to point to the

beginning of that memory segment When a pointer is declared, it is

uninitialized (like a regular variable) Unlike pointers, the value of an array

variable cannot be changed