40
1 دا ام خ ن ب ی س ی و ن ه م ا رن ب ان ن زC (21814 ( Lecture 11 Pointers

بنام خدا زبان برنامه نویسی C (21814 ( Lecture 11 Pointers

Embed Size (px)

DESCRIPTION

بنام خدا زبان برنامه نویسی C (21814 ( Lecture 11 Pointers. زبان برنامه نویسی C (21814 (. فصل دوازدهم: مقدمه ای بر Pointer ها در این فصل مروری بر آخرین بخش با قیمانده از انواع ابتدایی data type ها، یعنی Pointer ها انجام می شود. - PowerPoint PPT Presentation

Citation preview

Page 1: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

1

خدا بنام

نویسی برنامه )C (21814زبان

Lecture 11

Pointers

Page 2: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

2

نویسی برنامه )C (21814زبان

: بر ای مقدمه دوازدهم فصلPointerها

با • بخش آخرین بر مروری فصل این درابتدایی انواع از یعنی data typeقیمانده ها،

Pointer. شود می انجام ها

وظیفه • شد، اشاره قبال که همانطورPointer یک آدرس به در data objectاشاره

. است حافظه

گیری • بکار صحیح نحوه فصل این ها Pointerدراپراتور سه یعنی و آنها، مورد &,*, =اصلی

. گیرند می قرار بررسی

Page 3: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

3

نویسی برنامه )C (21814Pointersزبان

گیری • بکار توان Pointer Parametersبا مییک از نتیجه یک از .functionبیش گرفت

• Pointer محاوره زبان در ضمیر همانند نقشیمتفاوتی شی به لحظه هر در تواند می و دارد

. گیری بکار دالیل کند زبان Pointerاشاره در هاC: از عبارتند

یک – از مقدار یک از بیش functionدریافت

پردازش – و هاstringایجادها – آرایه روی به بهتر کنترلمی data structureایجاد – تغییر آنها اندازه که هایی

کند.

Page 4: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

4

نویسی برنامه )C (21814Pointersزبان

از • زبان Pointerاستفاده افزایش Cدر موجب . گردد می نویسی برنامه در انعطاف

متغیر pاگر • متغیر pointerیک آدرس و kباشدکه گفت توان می باشد، شده ذخیره آن به pدر

k کند می .اشاره• p points at k • p is reference to k• p refers indirectly to k

Page 5: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

5

نویسی برنامه )C (21814Pointersزبان

متغیرهای تعریف :pointerنحوهمتغیر • تعریف متغیر pointerبرای نوع ابتدا ،

که آنها pointerهایی به تواند می شده تعریف. شود می ذکر کند اشاره

نام • :pointerسپس شود می ذکر زیر بصورتint *p1, *p2;

از * • قبل عالمت از .p1استفاده الزامیست هایی pointerهر • متغیر نوع به تواند می فقظ

. کند اشاره شده تعریف که

Page 6: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

6

نویسی برنامه )C (21814Pointersزبان

به ابتدایی دهی ها pointerمقداریک • تعریف pointerاگر اولیه دهی مقدار بدون

بدون یابد می تخصیص آن به ای حافظه شود،لذا شود، ذخیره آن در آدرسی به pointerاینکه

خواهد اشاره شده ذخیره آن در قبل در چه هرنمود.

یک • همواره که باشید داشته به pointerتوجه. کند می اشاره جایی

اولیه • جایی pointerمقدار به اشاره تواند میدر تصادفی مکانی یا جاری برنامه در معنی بی

. باشد برنامه متن

Page 7: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

7

نویسی برنامه )C (21814Pointersزبان

به ابتدایی دهی ادامه))ها pointerمقدارهای • کامپایلر مورد ERRORپیغام Cاغلب در

یک اولیه .pointerمقدار کنند نمی ارسال

بر • برنامه اجرای هنگام مواقعی چنین دراتفاقات شده، ذخیره اولیه مقدار اساس

. دهد روی است ممکن مختلفی

از • است بهتر مشکل این از جلوگیری برایNULL pointer . شود استفاده

Page 8: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

8

نویسی برنامه )C (21814Pointersزبان

• NULL pointer درstdio.h نقش و شده تعریفبرای صفر مساوی اولیه دهی مقدار همان

. دارد را ها متغیر

مقدار • شود، pointerدر NULLاگر ذخیرهدر ) pointerآنگاه کند نمی اشاره جا هیچ به

به کند memory Location 0 واقع می .)اشاره

int *pt = NULL;

Page 9: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

9

نویسی برنامه )C (21814Pointersزبان

متغیر • یک pointerیک که است در pointerمکانی. شود می ذخیره آن

شد، • ذکر که یک pointerهمانطور آدرس خودش . بصورت کند می ذخیره خود در را دیگر متغیر

: متغیر یک تر آدرس pointerصریح یک شاملرا خودش آدرس متغیر خود که حالی در است

دارد.

نقش • اوقات برنامه pointerگاهی برای حتی هاصفحه ) است کننده گیج ای حرفه 445نویسان

کتاب).

Page 10: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

10

نویسی برنامه )C (21814Pointersزبان

شد، • دیده قبال که متغیر k&همانطور kآدرس. دهد می نشان را

که * • ، شود، indirectionاپراتور می نامیدهاپراتور مخالف . &عملی دهد می انجام را

واقع • مرجع p*در معنی که ) pبه به pمتغیری: . ( لذا باشد می کند می اشاره آن

*&k == k

*&p == p

Page 11: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

11

نویسی برنامه )C (21814Pointersزبان

دهد p *عبارت • می انجام را کاری همان دقیقا . دهد می انجام اسم با ضمیر یک که

متغیر pاگر • باشد، kآدرس کرده ذخیره را :آنگاه

m =*p ~ m = k

*p = n ~ k = nرا • متغیری نام خواهیم می که جا هر واقع در

یک از توانیم می آن pointerبنویسیم، به که. نماییم استفاده کند می اشاره متغیر

Page 12: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

12

نویسی برنامه )C (21814Pointersزبان

مثال:

int k, m;

int *p;

p = &k;

m = *p + 2; ~ m = k + 2;

Page 13: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

13

نویسی برنامه )C (21814Pointersزبان

یک تخصیص pointer:نحوهتخصیص • برای کلی روش وجود pointerسه

دارد:.1 : صریح ; p = &kتخصیص

یک 2. توسط مقدار یک به و pointerدسترسیمتغیر یک به آن مقدار اختصاص

m = *p;

که 3. متغیری به مستقیم غیر دهی مقدارpointer . کند می اشاره آن ; p = m+2 *به

Page 14: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

14

نویسی برنامه )C (21814Pointersزبان

مثال: #include <stdio.h>

void main(void) {

int *pt =NULL;

int *p;

int k=17,m;

p=&k;

pt=p;

m=*p +2;

*p = m;

printf( " address of p: %i contents of p: %i\n", (int) &p, (int)p);

printf( " address of pt: %i contents of pt: %i\n", (int) &pt, (int)pt);

printf( " address of k: %i contents of k: %i\n", (int) &k, (int)k);

printf( " address of m: %i contents of m: %i\n", (int) &m, (int)m);

printf( "\n\n");

}

Page 15: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

15

نویسی برنامه )C (21814Pointersزبان

• Yet another use for & and *• int year = 5; year represents the value stored in a

certain location (address( in memory.• The memory address, i.e., the address of year, is

called a pointer to year -- it gives the location of year, not it's value.

Page 16: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

16

نویسی برنامه )C (21814زبانThe 'address of' operator& ,

• Given int years; then &years is the memory address of this variable.

• &years is a constant pointer.• scanf("%d", &years) – scanf(( needs the

address of where to store the data.

Page 17: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

17

نویسی برنامه )C (21814زبانThe de-reference operator*

• An address of a variable can be called a reference to it.

• The * unary operator is the inverse of &.• It gets the thing referred to. It 'de-references'

the reference.• *&a is just the value of a

Page 18: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

18

نویسی برنامه )C (21814زبانPointer variables

• A variable that can hold an address is called a pointer variable.

• An integer pointer variable is declared as: int * p;

• int * is a new type called pointer to int• p is a variable of type pointer to int.• Only addresses of integers can be stored in p.

Page 19: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

19

نویسی برنامه )C (21814 زبانMore Syntax

int *p, years, a; years = 20;p = &years; // p holds address of yearsa = *p; // get value stored at p & store in a *p = 30; // years changed to 30printf("%d %d %d %d", years, a, *p, p);

Output: 30 20 30 1245000

Page 20: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

20

نویسی برنامه )C (21814زبان* on left vs. * on right of=

• *p on right side of assignment means: – "get the value to which p points"– ex: a = *p;

• *p on left side of assignment means:– "store the value of assignment in the location

pointed to by p"– ex: *p = 30;

Page 21: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

21

نویسی برنامه )C (21814زبانWhat’s it look like in memory

1245000:1245004:1245008:1245012:1245016:1245020:1245024:1245028:

int years, *p;years = 20;p = &years;

20 years

1245000 p

name valueyears 20&years 1245000 p 1245000&p 1245020*p 20

Page 22: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

22

نویسی برنامه )C (21814زبانChange 'years' using the pointer

int years, *p;years = 20;p = &years;*p = 10;

1245000:1245004:1245008:1245012:1245016:1245020:1245024:1245028:

10 years

1245000 p

Page 23: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

23

نویسی برنامه )C (21814زبانNot recommended for beginners!

int years, *p;years = 20;p = &years;*p = 10;++*p;

because equivalent to++(*p);

1245000:1245004:1245008:1245012:1245016:1245020:1245024:1245028:

11 years

1245000 p

Page 24: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

24

نویسی برنامه )C (21814زبانCall by Reference

• Give a function the address of a variable instead of it's value.

• Used to:1. Change values of variables in the caller's

function.

2. Return one or more values to the caller.

Page 25: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

25

نویسی برنامه )C (21814زبانCall by Reference (change values)

#include <stdio.h>void swapints(int *p, int *q); // note declaration

int main( void) {int a=2, b=5;printf("%d %d\n", a, b); // prints 2 5 swapints(&a, &b); // note actual parameters *printf("%d %d\n", a, b); // now prints 5 2 return 0;

}void swapints( int *p, int *q) {

int tmp;tmp = *p; // save a copy of what was in *p *p = *q; // now *p holds what *q holds *q = tmp; // swap is complete

}

Page 26: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

26

نویسی برنامه )C (21814 زبانOn Entering swapints

1000:1004:1008:1016:1020:1024:1028:1032:1036:1040:1044:1048:

a

p

2

1000

b

qtmp

1004

5void swapints( int *p, int *q( {

int tmp;tmp = *p; *p = *q;*q = tmp;

}

Page 27: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

27

نویسی برنامه )C (21814زبانFirst Statement

void swapints( int *p, int *q( {

int tmp;

tmp = *p; *p = *q;*q = tmp;

}

1000:1004:1008:1016:1020:1024:1028:1032:1036:1040:1044:1048:

a

p

2

1000

b

qtmp

1004

5

2

Page 28: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

28

نویسی برنامه )C (21814زبانSecond Statement

void swapints( int *p, int *q( {

int tmp;tmp = *p;

*p = *q;*q = tmp;

}

1000:1004:1008:1016:1020:1024:1028:1032:1036:1040:1044:1048:

a

p

5

1000

b

qtmp

1004

5

2

Page 29: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

29

نویسی برنامه )C (21814 زبان

Last Statement

void swapints( int *p, int *q( {

int tmp;tmp = *p;

*p = *q;

*q = tmp;}

1000:1004:1008:1016:1020:1024:1028:1032:1036:1040:1044:1048:

a

p

5

1000

b

qtmp

1004

2

2

Page 30: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

30

نویسی برنامه )C (21814زبانPointers and Arrays

• The inventors of C did a very clever thing.

• The name of an array is a constant pointer to the first element.

• Given int odds[5] = {3,5,7,9,11};

• We can use the name odds as a constant pointer to the beginning of the array.

• Let’s look at some of the possibilities.

Page 31: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

31

نویسی برنامه )C (21814زبانMemory picture of an Array

int odds[] = {3,5,7,9,11};

Thus odds has value 1000

1000:1004:1008:1016:1020:1024:1028:1032:1036:1040:1044:1048:

odds357911

Page 32: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

32

نویسی برنامه )C (21814زبانPointer Heaven (Hell)

int odds[ ] = {3,5,7,9,11}, *p, n;

p = odds; // p points to 3

p = &odds[0]; // same thing

n = *p; // n is now 3

n = p[0]; // same thing—array notation //on pointers (to arrays)

p[4] = 13; // odds[4] is now 13

Page 33: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

33

نویسی برنامه )C (21814زبانPointer Arithmetic Example

int odds[5] = {3,5,7,9,11}, *p, n;

p = odds; // p points to 3

++p; // p points to 5

p = (odds + 1); // p points to 5

p = (odds + 4); // p points to 11

p = &odds[4]; // same thing, points to 11

Page 34: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

34

نویسی برنامه )C (21814زبانPointer Arithmetic Rule

• The unit change in a pointer in arithmetic operations is equal to the size of the variable type it points to.pointers to int change in units of sizeof(int)

pointers to float change in units of sizeof(float)

pointers to double change in units of sizeof(double)

pointers to char change in units of sizeof(char)

Page 35: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

35

نویسی برنامه )C (21814زبانArray Parameters for Functions

• In prototype or definition, type required.

• In call to function, only name of array.

• Given declaration int array[5];float ave(float array[], int n) // prototype/def

float ave(float *array, int n) // same thing

x = ave(array, n); // call

Page 36: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

36

نویسی برنامه )C (21814زبانCall by Reference (Return Mult. Values)

void minMax(int a[], int n, int *min, int *max) {int i;// initialize to a value in the array*min= *max = a[0];for(i=0; i < n; i++) {

if ( a[i] > *max) //if larger, replace with new value*max = a[i];

if(a[i] < *min) //if smaller, replace with new value*min = a[i];

}return;

}

Page 37: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

37

نویسی برنامه )C (21814زبانIcons and Functions

• 2-D array passing by reference.

• Read icon data array from a file.

• Use a function to draw an icon.

Page 38: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

38

نویسی برنامه )C (21814زبانIconFunctions - readData

void readIcon(FILE *fp, char icon[][ICONSIZE]) {

int row, col;

// loop through all rows and columns

for(row = 0; row < ICONSIZE; row++)

for(col = 0; col < ICONSIZE; col++)

icon[row][col] = fgetc(fp); // store char in icon

fgetc(fp); // discard the newline at the end

}

Page 39: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

39

نویسی برنامه )C (21814زبانIconFunctions - drawIcon

void drawIcon (char icon[][ICONSIZE], int x, int y) {int row, col;

// draw each pixel of the icon for (row = 0; row < ICONSIZE; row++)

for (col = 0; col < ICONSIZE; col++) {_gotoxy(x + col, y + row);printf("%c", icon[row][col]);

}}

Page 40: بنام خدا زبان برنامه نویسی   C  (21814 ( Lecture 11 Pointers

40

نویسی برنامه )C (21814 زبانIconFunctions main

int main( void ) {char icon[ICONSIZE][ICONSIZE];FILE *fp; int i;

// open the data file if ((fp = fopen("icondata.txt", "r")) == NULL){

printf("Can't open the icon file");exit (1);// if can't open, have to quit

}readIcon(fp, icon); // read pixels into icon array for (i=0; i < 10; ++i) {

drawIcon(icon, 6*i, 2*i); // draw icon in diag. row }fclose(fp);return 0;

}