190 likes | 279 Views
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
E N D
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 • pointer variable: a variable dedicated to hold addresses.
& 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.
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;
Pointer Assignment • &<varName> /* address of <varName> */ iVarPtr = &iVar; ptr2 = iVarPtr; myFloatPointer = &f; iVarPtr = iVar; /* BAD... */ myFloatPointer = iVarPtr; /* INVALID */
NULL • a pointer value of 0 is known as NULL pointer. int * x; x = NULL;
Dereferencing pointers • & <ptrVarName> /* value inside the memory pointed at */ iVar = * iVarPtr; f = * iVarPtr; f = * myFloatPtr;
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 );
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);
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); ...
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); }
float *maxPtr(float *xp, float *yp){ return *xp > *yp ? xp : yp; } void main(){ float a, b, *c; c = maxPtr(&a, &b); ...
Goal • First read 10 numbers. • Then read numbers 1<= x <= 10 and tell what the xth number was.
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); ....
Arrays int arr [10]; int i, x; for(i=0; i<10; i++) scanf("%d", &arr[i]); scanf("%d", &x); printf("%d", arr[x]);
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.
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 */