Pointers

37
Pointers 1 Department of Computer Science-BGU 21:14:37

description

Pointers. כתובת של משתנה. נניח משתנים: double x; //8bytes short y ;//2bytes char c ;//1 bytes כאשר בזמן הריצה של התוכנית המחשב יקצה זיכרון למשתנים. לכל תא זיכרון יש ערך מספרי המציין את מיקומו המדויק בזיכרון המחשב. ערך זה מכונה כתובת זיכרון ( memory address ). - PowerPoint PPT Presentation

Transcript of Pointers

Page 1: Pointers

Pointers1

Department of Computer Science-BGU 19:19:59

Page 2: Pointers

כתובת של משתנה

נניח משתנים: double x; //8bytes

short y ; //2bytes char c ; //1 bytes

כאשר בזמן הריצה של התוכנית המחשב יקצה זיכרון למשתנים.

לכל תא זיכרון יש ערך מספרי המציין את מיקומו המדויק memoryבזיכרון המחשב. ערך זה מכונה כתובת זיכרון )

address.) הינה הכתובת של הבית הראשון משתנהכתובת הזיכרון של

ברצף הבתים שמשתנה זה תופס בזיכרון. למשל בדוגמא : y ושל 1000 היא xהכתובת של 1008.

2

Department of Computer Science-BGU 19:20:00

Page 3: Pointers

כתובת של משתנה

& האופרטור אם x הוא משתנה אזי &x היא כתובת הזיכרון של x כלומר ,

".… מציין "כתובתו של&האופרטור : ובדוגמא שלנו&x = 1000&y = 1008&c = 1010  כתובות זיכרון אלו הינם ביטויים בעליי ערך וטיפוס. הטיפוס של

כתובת הזיכרון של משתנה כלשהו נגזר מהמשתנה עצמו.

1000x1008y1010c

3

Department of Computer Science-BGU 19:20:00

Page 4: Pointers

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

4

Department of Computer Science-BGU 19:20:00

Page 5: Pointers

Using pointers

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.

5

Department of Computer Science-BGU 19:20:00

Page 6: Pointers

Using pointers

מצביע הוא משתנה אשר מכיל כתובת זיכרון. כשנרצה להצהיר על מצביע ל type התו נוסיף את' . משמאל למשתנה '

  לדוגמא :

int הגדרת מצביע ל int <identifier> ;

charהגדרת מצביע ל char <identifier>;

doubleהגדרת מצביע ל double <identifier> ;

 ברגע שנצהיר על משתנים מצביעים, נוכל להציב לתוכם כתובות של משתנים . לדוגמא נתונה התוכנית הבאה :

6

Department of Computer Science-BGU 19:20:00

Page 7: Pointers

Using pointers

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

P

174 3 4… …

833832 834 835 836 837 838 839 840 841

7

Department of Computer Science-BGU 19:20:01

Page 8: Pointers

האופרטור

The unary operator * is applied on pointers

Access the object the pointer points to The statements:

int * P;

int C;P = &C;

*P=5;

Puts in C (the variable pointed by P) the value 5

8

Department of Computer Science-BGU 19:20:01

Page 9: Pointers

האופרטור

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

9

Department of Computer Science-BGU 19:20:01

Page 10: Pointers

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;

10

Department of Computer Science-BGU 19:20:01

Page 11: Pointers

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

But address is a value !! And we can pass the address as argument to functions

Remember ?!void swap(int x, int y) {

…swap x and y… }

11

Department of Computer Science-BGU 19:20:01

Page 12: Pointers

How can we fix it?

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

}void main() {

int a=7,b=8;printf("a = %d, b = %d\n", a, b);swap(&a,&b);printf("a = %d, b = %d\n", a, b);

}

12

Department of Computer Science-BGU 19:20:01

Page 13: Pointers

Back to scanf

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.

13

Department of Computer Science-BGU 19:20:01

Page 14: Pointers

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]

14

Department of Computer Science-BGU 19:20:01

Page 15: Pointers

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).

15

Department of Computer Science-BGU 19:20:01

Page 16: Pointers

Pointer arithmetic

Pointers can be incremented and decremented.If p is a pointer to a particular type, p+1 yields

the correct address of the next variable of the same type.

Is it possible ?? YES If int * p ;

p +1 means p + sizeof(int)p++, p+i, and p += i also make same sense.

p + 4 means p + (4 * sizeof(int))

16

Department of Computer Science-BGU 19:20:01

Page 17: Pointers

What is this ‘sizeof? ’

The sizeof operator gets a variable or a type as an input and outputs its size in bytes:

double x; s1=sizeof(x); /* s1 is 8 */ s2=sizeof(int) /* s2 is 4 */

17

Department of Computer Science-BGU 19:20:01

Page 18: Pointers

Pointer arithmetic

What about arrays ?? They are pointers !!Unlike pointers, the value of an array

variable cannot be changed.We can write :

int a[10];int * p;p = a;

But the arrays are constants !! So …p++ ; // Correcta++; // Error !!

18

Department of Computer Science-BGU 19:20:01

Page 19: Pointers

Pointer arithmetic - example

If p and q point to elements in an array, q-p yields the number of elements between p and q.

However, there is a difference between pointer arithmetic and “regular” arithmetic.

A better example – strcpy.c

19

Department of Computer Science-BGU 19:20:01

Page 20: Pointers

An additional use

A function that accepts an array and searches for something within it (a char, a number, etc.)

What should the function return? The index where the ‘something’ was found A pointer to the place where it was found

20

Department of Computer Science-BGU 19:20:01

Page 21: Pointers

Functions that return pointers

Like any other data type, functions can return pointers

For example, a prototype for a function returning a pointer to char will be –char *func(…);

But how would we indicate failure Suppose we searched the array and didn’t find

anything – what should we return?

21

Department of Computer Science-BGU 19:20:01

Page 22: Pointers

The NULL pointer

The NULL pointer is an ‘empty’ pointer that points to nothing.

It is the address number 0.If a pointer p is set to NULL, trying to access

*p results in a run-time (not compilation) error.

Often used to indicate failure

22

Department of Computer Science-BGU 19:20:01

Page 23: Pointers

Example- solving quadratic equation - the "easy way"

#include <stdio.h>#include <math.h>

int quad(double ,double ,double ,double* ,double* );void main{)(

double a,b,c,x1,x2; int res;

scanf("%lf%lf%lf",&a,&b,&c); res=quad(a,b,c,&x1,&x2);

switch(res){ case 0 : printf("No solution\n");

break; case 1 : printf("x1=x2=%.2f\n",x1);

break; case 2 : printf("x1=%.2f , x2=%.2f\n",x1,x2);

} }  

23

Department of Computer Science-BGU 19:20:01

Page 24: Pointers

Function quad(……)

/* we assume a!=0 the function return : 0 no solution (we neglect the complex

option) 1 two equal roots 2 two different roots*/int quad(double a ,double b ,double c ,double* px1 ,double* px2){ double d = (b*b - 4*a*c);  if(d < 0) return 0; if(d==0){ *px1 = *px2 = -b/(2*a); return 1; } *px1= (-b + sqrt(d))/(2*a); *px2= (-b - sqrt(d))/(2*a);  return 2;}

24

Department of Computer Science-BGU 19:20:01

Page 25: Pointers

Usefully functions

קיימות פונקציות ספריה לטיפול במחרוזות ותווים. על מנת Cבשפת להשתמש בפונקציות אלו יש להוסיף בתחילת התוכנית את שורות:

 #include <string.h>

#include <ctype.h>

דוגמאות לפונקציות לבדיקת תווים : 

int isdigit(int) האם התו הוא סיפרה

int isspace(int) האם התו הוא רווח int isupper(int) האם התו הוא אות גדולה

int islower(int) האם התו הוא אות קטנה לפונקציות נוספות עיינו בספרות .

 

25

Department of Computer Science-BGU 19:20:02

Page 26: Pointers

Some functions of string.h

int strncmp(char *s1, char *s2, int n) - returns 0 if the first n letters of s1 are equal to those of s2, and returns non-zero if first n letters are different

char * strchr(char * str, char c)– returns a pointer to the first occurrence of a character within a string

char *strstr(char * st1,char * st2) – returns a pointer to the first occurrence of st1 string within the st2. r

void strncpy(char * st1, char * st2, int n) copies the first n letters of st2 to the address of st1. No copy the ‘\0’ letter to st1 !!

26

Department of Computer Science-BGU 19:20:02

Page 27: Pointers

Mystery - What does this do?

char *rec_stam(char * str, char c) {if (*str == '\0')

return NULL;if (*str == c)

return str;return rec_stam(str+1, c);

}A recursive implementation of strchrReturn pointer to the first instance of

the letter c into the string str

27

Department of Computer Science-BGU 19:20:02

Page 28: Pointers

Again mystery – what does this do?

void main() {char s[101];char *p;scanf("%100s", s);p = strchr(s, ',');while(p!=NULL) {

*p = ' '; p = strchr(p+1, ',');

}printf("The result is - %s\n", s);

}

28

Department of Computer Science-BGU 19:20:02

Page 29: Pointers

Exercise

Write a recursive implementation of strcmp – Input – two strings Output – 0 if both are equal, 1 if not

Write a program that accepts two strings from the user and checks whether they are equal

29

Department of Computer Science-BGU 19:20:02

Page 30: Pointers

Solution

int strcmp_r(char *s, char *t) { if (*s == '\0' || *s != *t)

return *s - *t; else return(strcmp_r(s+1, t+1));

}

30

Department of Computer Science-BGU 19:20:02

Page 31: Pointers

Dynamic Memory

Allocation31

Department of Computer Science-BGU 19:20:02

Page 32: Pointers

Dynamic Memory Allocation

Array variables have fixed size, used to store a fixed and known amount of variables

This size can’t be changed after compilation

However, we don’t always know in advance how much space we would need for an array or a variable

We would like to be able to dynamically allocate memory

32

Department of Computer Science-BGU 19:20:02

Page 33: Pointers

The malloc function

void *malloc(unsigned int nBytes);

The function malloc is used to dynamically allocate nBytes worth of space. How to determine nBytes?

malloc returns a pointer to the allocated area on success, NULL on failure.

You should always check whether memory was successfully allocated.

Remember to #include <stdlib.h>

33

Department of Computer Science-BGU 19:20:02

Page 34: Pointers

Why casting?

The type void * specifies a general pointer, which can be cast to any pointer type.

Casting is needed because malloc returns void *:

void *malloc(unsigned int nbytes);

Casting for integer :y=(int *) malloc(n*sizeof (int));

34

Department of Computer Science-BGU 19:20:02

Page 35: Pointers

Free the allocated memory segment

void free(void *ptr);

We use free(p) to free the allocated memory pointed to by pIf p doesn’t point to an area allocated by malloc, a run-time error occursAlways remember to free the allocated memory once you don’t need it anymoreOtherwise, you may run out of memory before you know it!

35

Department of Computer Science-BGU 19:20:02

Page 36: Pointers

Array of pointers

Problem : Write program that receive strings from a user

and print these strings in a lexicography order.

Solution :Array of pointers to char (malloc)

char * names[256] or char ** names;

This is not the same declaration !!

19:20:02Department of Computer Science-BGU

36

Page 37: Pointers

Array of pointers

Declaration:char ** names;int n,i;scanf(“%d”,&n);names = (char **)malloc(n * sizeof(char *));for(i=o; i<n; i++){

char[i] = (char*)malloc(256*sizeof(char));

}// bubble on the pointers !!

19:20:03Department of Computer Science-BGU

37