Download presentation
Presentation is loading. Please wait.
1
Lecture 07 METU Dept. of Computer Eng. Summer 2002 Ceng230 - Section 01 Introduction To C Programming by Ahmet Sacan Mon July 22, 2002
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
contents:.... x107 3...... address:.... x103x104x105x106x107... variable name: apa ip*ipa&a x10733
5
Pointer declaration * ; int iVar;/* declares an int. */ int * iVarPtr;/* declares a variable that will POINT to an integer */ int * ptr2; float f; float * myFloatPointer;
6
Pointer Assignment & /* address of */ iVarPtr = &iVar; ptr2 = iVarPtr; myFloatPointer = &f; iVarPtr = iVar;/* BAD... */ myFloatPointer = iVarPtr; /* INVALID */
7
NULL a pointer value of 0 is known as NULL pointer. int * x; x = NULL;
8
Dereferencing pointers & /* value inside the memory pointed at */ iVar = * iVarPtr; f = * iVarPtr; f = * myFloatPtr;
9
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 );
10
Functions & Pointers - Call by reference - int square(int x) { return x*x; } void main() {... a = square(a); void square(int * x) { *x = (*x)*(*x); } void main() {... square(&a); int a; void square( ) { a = a*a; } void main() {... square();
11
int Add(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);...
12
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); }
13
float *maxPtr(float *xp, float *yp){ return *xp > *yp ? xp : yp; } void main(){ float a, b, *c; c = maxPtr(&a, &b);...
14
Goal First read 10 numbers. Then read numbers 1<= x <= 10 and tell what the xth number was.
15
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);....
16
Arrays int arr [10]; int i, x; for(i=0; i<10; i++) scanf("%d", &arr[i]); scanf("%d", &x); printf("%d", arr[x]);
17
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.
18
Array Declaration [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 */
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.