Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pointers הרצאה קריטית. השאלות הפתוחות מה זה ה- & שמופיע ב scanf מדוע כשמעבירים מחרוזת ל scanf אין צורך ב & האם ניתן להכריז על מערך שגדלו אינו ידוע בתחילת.

Similar presentations


Presentation on theme: "Pointers הרצאה קריטית. השאלות הפתוחות מה זה ה- & שמופיע ב scanf מדוע כשמעבירים מחרוזת ל scanf אין צורך ב & האם ניתן להכריז על מערך שגדלו אינו ידוע בתחילת."— Presentation transcript:

1 Pointers הרצאה קריטית

2 השאלות הפתוחות מה זה ה- & שמופיע ב scanf מדוע כשמעבירים מחרוזת ל scanf אין צורך ב & האם ניתן להכריז על מערך שגדלו אינו ידוע בתחילת התכנית? כיצד ניתן לשנות את ערכי הפרמטרים בפונקציה? למה יש הבדל בין משתנים פשוטים למערכים? האם ניתן להחזיר יותר מפרמטר אחד בפונקציה?

3 What is the matrix?

4 void main() { int a; char ch; int arr[4]={2}; a=17; ch=25; a=ch; ch=arr[0]; arr[2]=a*ch; arr[3]=arr[2]; } MemoryAddresses 0 3055 2012 a: 2012 22 1052 ch: 1052 2 0 0 0 arr: 3020 3028 3020 3032 3024 17 25 2 50

5 הגדרות פוינטרים &a : הכתובת של a (כתוב בטבלת הכתובות) ל * יש שני תפקידים: –בזמן הגדרת משתנה: int *p משמעו שהטיפוס של p הוא מצביע ל int (ויכיל כתובת) –בזמן ביצוע פעולה: *p משמעו: תוכן התא שכתובתו רשומה ב p

6 void main() { int a,b,*pa,*pb; a = 15; b = 20; pa = &a; b = *pa; *pa = 0; pb = &b; pa=pb; *pa=5; } MemoryAddresses 0 7663 5556 a: 5556 359 524 b: 524 2 8797 2015pa: 3028 3028 6080 15 5556 0 5 524 pb: 6080

7 void main() { int a[6],*p,*q,i; p = &a[0]; *p = 50; q = &a[1]; *(p+2) = 7; *q=9; *(q+3)=*(p+1); i=q-p; } MemoryAddresses 0 35 5572 a: 5572 17 528 p: 528 2 10 5572 7 q: 3028 3028 6080 50 5576 9 9 i: 6080 59 7663 3 13 1

8 swap פונקצית #include void swap(int x, int y){ int temp=x; x=y; y=temp; } void main(){ int a=5,b=7; printf("a: %d, b: %d\n",a,b); swap(a,b); printf("a: %d, b: %d\n",a,b); } לא עבר

9 swap פונקצית #include void swap(int *x, int *y){ int temp=*x; *x=*y; *y=temp; } void main(){ int a=5,b=7; printf("a: %d, b: %d\n",a,b); swap(&a,&b); printf("a: %d, b: %d\n",a,b); } עבר

10 החזרת יותר מפרמטר אחד #include void minmax(int x, int y, int * min, int * max){ if (x<y){ *min=x; *max=y; } else{ *min=y; *max=x; } void main(){ int a,b,min,max; printf("Enter a and b\n"); scanf("%d%d",&a,&b); minmax(a,b,&min,&max); printf("min: %d, max: %d\n",min,max); }

11 swap שימוש ב void minmax(int x, int y, int * min, int * max){ *min=x; *max=y; if (x>y) swap (min, max); }

12 קצת רקורסיה #include int *binSearch (int value, int *from, int *to){ int *middle = from+(to-from)/2; if (*middle==value) return middle; else if (from == to) return NULL; else if (*middle < value) return binSearch (value, middle+1, to); else return binSearch (value, from, middle-1); }

13 שימוש void main(){ int a[10]={5,7,15,19,28,35,56,88,155,156}; int * p, index; p=binSearch(19,&a[0],&a[9]); if (p==NULL) index=-1; else index=p-a; printf ("%d\n",index); }

14 קלט / פלט void main(){ int a, b, *pa; char st[10]; pa=&a; scanf("%d%d%s", pa, &b, st); printf("%d %d %c\n",*pa, b, st[3]); printf("%d %d %c\n",a, *(&b), *(st+3)); }

15 הקצאת זיכרון דינאמית #include void main(){ int *arr, n, i; printf ("Enter size of array\n"); scanf("%d",&n); arr=malloc(n*sizeof(int)); if (arr==NULL){ printf ("Failed to allocate memory\n"); return; } for (i=0; i<n; i++){ scanf("%d",arr+i); } free(arr); }


Download ppt "Pointers הרצאה קריטית. השאלות הפתוחות מה זה ה- & שמופיע ב scanf מדוע כשמעבירים מחרוזת ל scanf אין צורך ב & האם ניתן להכריז על מערך שגדלו אינו ידוע בתחילת."

Similar presentations


Ads by Google