Presentation is loading. Please wait.

Presentation is loading. Please wait.

Ahmet Sacan - Ceng302 - 20040520 1 C-Programming: A Crash-Course in Pointers, Dynamic Memory, Structs, & Files Ceng302: Intro to DBMS - 20032 Ahmet Sacan.

Similar presentations


Presentation on theme: "Ahmet Sacan - Ceng302 - 20040520 1 C-Programming: A Crash-Course in Pointers, Dynamic Memory, Structs, & Files Ceng302: Intro to DBMS - 20032 Ahmet Sacan."— Presentation transcript:

1 Ahmet Sacan - Ceng302 - 20040520 1 C-Programming: A Crash-Course in Pointers, Dynamic Memory, Structs, & Files Ceng302: Intro to DBMS - 20032 Ahmet Sacan 20050420

2 Ahmet Sacan - Ceng302 - 20040520 2 The Memory content address content address content address content address

3 Ahmet Sacan - Ceng302 - 20040520 3 Variable Boxes int x, *p; int x, *p; type: int name: x content address content address content address content address type: int* name: p

4 Ahmet Sacan - Ceng302 - 20040520 4 Pointers A pointer is variable which points to a place in computer's memory. A pointer is variable which points to a place in computer's memory. pointer = address pointer = address pointer variable: a variable dedicated to hold addresses. pointer variable: a variable dedicated to hold addresses.

5 Ahmet Sacan - Ceng302 - 20040520 5 int x; int x; x=35; x=35; ? ? ? xint 06510 35 xint 06510

6 Ahmet Sacan - Ceng302 - 20040520 6 int *p; int *p; p=06582; p=06582; 35 xint 06510 35 xint 06510 06582 pint* 06571 06582 ? ? ? pint* 06571 ? ? ?

7 Ahmet Sacan - Ceng302 - 20040520 7 & and * unary operators &, when applied to a variable, yields its address (pointer to the variable) &, when applied to a variable, yields its address (pointer to the variable) *, when applied to an address (pointer), fetches the value at that address. *, when applied to an address (pointer), fetches the value at that address. can use & for any variable. But use * only for pointers! can use & for any variable. But use * only for pointers!

8 Ahmet Sacan - Ceng302 - 20040520 8 p = &x; p = &x; *p = 13; *p = 13; 35 xint 06510 pint* 06571 06510 13 xint 06510 pint* 06571 06510

9 Ahmet Sacan - Ceng302 - 20040520 9 contents:....x107 3...... address:....x103x104x105x106x107... variable name: px p*px&xx10733x107

10 Ahmet Sacan - Ceng302 - 20040520 10 Pointer declaration * ; * ; int iVar;/* declares an int. */ int * iVarPtr;/* declares a variable that will POINT to an integer */ int * ptr2; float f; float * myFloatPointer;

11 Ahmet Sacan - Ceng302 - 20040520 11 Pointer Assignment & /* address of */ & /* address of */ iVarPtr = &iVar; ptr2 = iVarPtr; myFloatPointer = &f; iVarPtr = iVar;/* BAD... */ myFloatPointer = iVarPtr; /* INVALID */

12 Ahmet Sacan - Ceng302 - 20040520 12 Dereferencing pointers * /* value inside the * /* value inside the memory pointed at */ memory pointed at */ iVar = * iVarPtr; f = * iVarPtr; f = * myFloatPtr;

13 Ahmet Sacan - Ceng302 - 20040520 13 int x=3, y=4, *p1=NULL; int *p2=0; p1 = &x; printf("%d %xd %xd %d", x, &x, p1, *p1); x=5; *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 ); &p1, p1, *p1, &p2, p2, *p2 ); 4 yint 06514 0 p1int* 06518 0 3 xint 06510 0 p2int* 06522 0

14 Ahmet Sacan - Ceng302 - 20040520 14 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();

15 Ahmet Sacan - Ceng302 - 20040520 15 int Add(int x, int y){ return x+y; } void main(){...... c = Add(a, b); c = Add(a, b);... void Add(int x, int y, int *z) { *z = x+y; } void main(){...... Add(a, b, &c); Add(a, b, &c);...

16 Ahmet Sacan - Ceng302 - 20040520 16

17 Ahmet Sacan - Ceng302 - 20040520 17 C-Programming: A Crash-Course in Pointers, Dynamic Memory, Structs, & Files

18 Ahmet Sacan - Ceng302 - 20040520 18 Dynamic Memory Allocation When you do not know at compile-time (when you are writing your program) how much memory you will need, then you have to use malloc to allocate memory at run time as needed. When you do not know at compile-time (when you are writing your program) how much memory you will need, then you have to use malloc to allocate memory at run time as needed. void * malloc(size_t memorySize) void * malloc(size_t memorySize) free the memory when it is no longer needed. free the memory when it is no longer needed. free(memoryLoc) free(memoryLoc)

19 Ahmet Sacan - Ceng302 - 20040520 19 int x, *p; int x, *p; xintp int* malloc(2*sizeof(int)); malloc(2*sizeof(int)); xintp int* int

20 Ahmet Sacan - Ceng302 - 20040520 20 p = (int*) malloc(3*sizeof(int)); p = (int*) malloc(3*sizeof(int)); xintp int* int

21 Ahmet Sacan - Ceng302 - 20040520 21 int a [3], *p; int a [3], *p; p = (int*) malloc(3*sizeof(int)) p = (int*) malloc(3*sizeof(int)) p int* x int[3] x[0]x[2]x[1]p[0]p[2]p[1] int

22 Ahmet Sacan - Ceng302 - 20040520 22 p = (int*) malloc(3*sizeof(int)) p = (int*) malloc(3*sizeof(int)) p int* free(p); free(p); p int*

23 Ahmet Sacan - Ceng302 - 20040520 23 int x[3][2]; x int[3][2] x[1][1]x[2][1] x int[3][2] x[0] x[1] x[2] x[..][0] x[..][1] x[2][1]

24 Ahmet Sacan - Ceng302 - 20040520 24 int **p; p = (int**) malloc(3*sizeof(int*)); for(i=0; i<3; i++) p[i] = (int*) malloc(2*sizeof(int)); p int** int* int p[1][1] int p[2][1]

25 Ahmet Sacan - Ceng302 - 20040520 25

26 Ahmet Sacan - Ceng302 - 20040520 26 C-Programming: A Crash-Course in Pointers, Dynamic Memory, Structs, & Files

27 Ahmet Sacan - Ceng302 - 20040520 27 structs a struct allows collecting primitive data- types to construct your own data-type. a struct allows collecting primitive data- types to construct your own data-type. typedef struct{ int x; int x; int y; int y; } Nokta; Nokta xint y

28 Ahmet Sacan - Ceng302 - 20040520 28 after declaring a struct data-type, you can declare variables of the struct type just like any other variable type. There is some difference in the initialization. after declaring a struct data-type, you can declare variables of the struct type just like any other variable type. There is some difference in the initialization. Nokta zeroCoordinate = {0,0}; Nokta birNokta; Nokta *aNoktaPointer, *pp; pp = &birNokta;

29 Ahmet Sacan - Ceng302 - 20040520 29 Accessing struct members: dot operator You can access the member of a struct variable via the "." operator. You can access the member of a struct variable via the "." operator. int a,b,c; a = birNokta.x; b = birNokta.y; c = (*pp).x; -> operator: shortcut for pointers. -> operator: shortcut for pointers. (*p).x is equivalent to p->x (*p).x is equivalent to p->x c = pp->x;

30 Ahmet Sacan - Ceng302 - 20040520 30 struct advantages. structs provide great semantic comfort to programs. structs provide great semantic comfort to programs. typedef struct{ int userID; int userID; char fname[16]; char fname[16]; char lname[16]; char lname[16]; float grade; float grade;}Student; Student studs[1000];

31 Ahmet Sacan - Ceng302 - 20040520 31 Vector struct in struct typedef struct{ Nokta start; Nokta end; } Vector; start Nokta xint y end Nokta xint y

32 Ahmet Sacan - Ceng302 - 20040520 32 Vector *v, *p; v = (Vector*) malloc(10*sizeof(Vector)); v[7].start.x = 3; v[7].start.y = 5; p= &(v[7]); (*p).end.x = 8; p->end.y = 13;

33 Ahmet Sacan - Ceng302 - 20040520 33

34 Ahmet Sacan - Ceng302 - 20040520 34 C-Programming: A Crash-Course in Pointers, Dynamic Memory, Structs, & Files

35 Ahmet Sacan - Ceng302 - 20040520 35 Predefined file-streams stdin: standard input stdin: standard input stdout: standard output stdout: standard output stderr: standard error stderr: standard error printf(...) is same as fprintf(stdout, …) printf(...) is same as fprintf(stdout, …) scanf(…) is same as fscanf(stdin, …) scanf(…) is same as fscanf(stdin, …)

36 Ahmet Sacan - Ceng302 - 20040520 36 file operations. Once you open a file, you can use fprintf and fscanf on these files, just like you use printf and scanf. Once you open a file, you can use fprintf and fscanf on these files, just like you use printf and scanf. fprintf(FILE * filehandler, char * formatstr,...); fscanf(FILE * filehandler, char * formatstr,...); the file-handler is either a predefined stream, or can be obtained by opening a file: the file-handler is either a predefined stream, or can be obtained by opening a file: FILE *fp; fp = fopen("myfile.txt", "r");....fclose(fp);

37 Ahmet Sacan - Ceng302 - 20040520 37 fopen, fclose Open & close a file for reading: Open & close a file for reading: fin = fopen(“myfile.txt”, “r”); fscanf(fin, “%d”, &x); …fclose(fin); Open & close a file for writing: Open & close a file for writing: fout = fopen(“myfile.txt”, “w”); fprintf(fout, "x is equal to: %d\n", x);...fclose(fout);

38 Ahmet Sacan - Ceng302 - 20040520 38 Quick Note - scanf, fscanf when reading a character, put a space before %c, if you want to skip spaces. when reading a character, put a space before %c, if you want to skip spaces. char mychar; scanf(" %c", &mychar); Can read a word with %s Can read a word with %s char str[64]; scanf(" %s", str);

39 Ahmet Sacan - Ceng302 - 20040520 39 Quick Note - strings strcmp compares two strings and returns 0 if they are equal. strcmp compares two strings and returns 0 if they are equal. #include #include int strcmp(char *s, char *t); if(!strcmp(cmd, "insert")) printf("cmd is insert\n"); else if(!strcmp(cmd, "delete")) printf("cmd is delete\n"); else printf("cmd [%s] is not recognized.\n", cmd);

40 Ahmet Sacan - Ceng302 - 20040520 40 Quick Note - bits >> operator shifts the bits to the right. >> operator shifts the bits to the right. So, x >> n shifts x, n bits to the right. say, x=26 (011010) then y = x >> 2; makes: y = 6 (000110) x>>n is same as dividing x by 2 n. x>>n is same as dividing x by 2 n. x<<n is same as multiplying x by 2 n. x<<n is same as multiplying x by 2 n.

41 Ahmet Sacan - Ceng302 - 20040520 41 Print bits /*prints in reverse, least-significant-bit first*/ void PrintBits(int x){ int b; while(x){ b = x - (x >> 1) > 1) << 1; x = x >> 1; printf("%d", b); }}

42 Ahmet Sacan - Ceng302 - 20040520 42


Download ppt "Ahmet Sacan - Ceng302 - 20040520 1 C-Programming: A Crash-Course in Pointers, Dynamic Memory, Structs, & Files Ceng302: Intro to DBMS - 20032 Ahmet Sacan."

Similar presentations


Ads by Google