1 / 19

Lecture 07

Mon July 22, 2002. Lecture 07. METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan. Pointers. A pointer is variable which points to a place in computer's memory. pointer = address

Télécharger la présentation

Lecture 07

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mon July 22, 2002 Lecture 07 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan

  2. Pointers • A pointer is variable which points to a place in computer's memory. • pointer = address • pointer variable: a variable dedicated to hold addresses.

  3. & and * unary operators • &, when applied to a variable, yields its address (pointer to the variable) • *, when applied to an address (pointer), fetches the value at that address.

  4. Pointer declaration • <type> * <varName> ; int iVar; /* declares an int. */ int * iVarPtr; /* declares a variable that will POINT to an integer */ int * ptr2; float f; float * myFloatPointer;

  5. Pointer Assignment • &<varName> /* address of <varName> */ iVarPtr = &iVar; ptr2 = iVarPtr; myFloatPointer = &f; iVarPtr = iVar; /* BAD... */ myFloatPointer = iVarPtr; /* INVALID */

  6. NULL • a pointer value of 0 is known as NULL pointer. int * x; x = NULL;

  7. Dereferencing pointers • & <ptrVarName> /* value inside the memory pointed at */ iVar = * iVarPtr; f = * iVarPtr; f = * myFloatPtr;

  8. int x=3, y=4, *p1=NULL; int *p2=0; p1 = &x; printf("%d %xd %xd %d", x, &x, p1, *p1); x=5; printf("%d %xd %xd %d", x, &x, p1, *p1); *p1 = 6; printf("%d %xd %xd %d", x, &x, p1, *p1); p2 = p1; printf("%xd %xd %d %xd %xd %d", &p1, p1, *p1, &p2, p2, *p2 );

  9. Functions & Pointers- Call by reference - int square(int x) { return x*x; } void main() { ... a = square(a); int a; void square( ) { a = a*a; } void main() { ... square(); void square(int * x) { *x = (*x)*(*x); } void main() { ... square(&a);

  10. intAdd(int x, int y){ return x+y; } void main(){ ... c = Add(a, b); ... void Add(int x, int y, int *z) { *z = x+y; } void main(){ ... Add(a, b, &c); ...

  11. void OrderSwap ( int *x, int *y ){ int t; if(*x > *y){ t = *x; *x = *y; *y = t; } } void main(){ int a, b, c; scanf("%d %d %d", &a, &b, &c); OrderSwap(&a,&b); OrderSwap(&b,&c); OrderSwap(&a,&b); printf("%d %d %d", a, b, c); }

  12. float *maxPtr(float *xp, float *yp){ return *xp > *yp ? xp : yp; } void main(){ float a, b, *c; c = maxPtr(&a, &b); ...

  13. Goal • First read 10 numbers. • Then read numbers 1<= x <= 10 and tell what the xth number was.

  14. int a, b, c, d, e, f, g, h, i, j; int x; scanf("%d %d %d %d %d %d %d %d %d %d %d", &a, &b, &c, &d, &e, &f, &g, &h, &i, &j); scanf("%d", &x); if(x==1) printf("%d", a); else if(x==2) printf("%d", b); ....

  15. Arrays int arr [10]; int i, x; for(i=0; i<10; i++) scanf("%d", &arr[i]); scanf("%d", &x); printf("%d", arr[x]);

  16. Arrays • array : a finite, ordered collection of the same-typed data items. • element: individual data items in an array • subscript (index): Only one name is assigned to an entire array. Individual elements are referenced by specifying an index. Subscripts start at ZERO.

  17. Array Declaration • <type> <arrayName> [expr-1]...[expr-n] • expr-i must be a constant integral type. int midtermScores[50]; #define NUM_EXAMS 3 float exams[3]; int x = 10; float prices[x]; /* INVALID! array-expr must be constant */

More Related