1 / 35

Introduction to Computing Lecture 12 Pointers

Introduction to Computing Lecture 12 Pointers. Dr. Bekir KARLIK Yasar University Department of Computer Engineering bekir. karlik@ yasar.edu.tr. Topics. Introduction to Pointers Pointers and Function Parameters. ch:. ’A’. 0x2000. Memory Address of a Variable. char ch = ’A’;.

tale
Download Presentation

Introduction to Computing Lecture 12 Pointers

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. Introduction to Computing Lecture 12Pointers • Dr. Bekir KARLIK • Yasar University • Department of Computer Engineering • bekir.karlik@yasar.edu.tr

  2. Topics • Introduction to Pointers • Pointers and Function Parameters

  3. ch: ’A’ 0x2000 Memory Address of a Variable char ch = ’A’; The memory address of the variable ch. The value of the variable ch.

  4. char ch = ’A’; ’A’ 0x2000 &ch yields the value 0x2000 The &Operator • Gives the memory address of an object • Also known as the “address operator.”

  5. “conversion specifier” for printing a memory address Example: char ch; printf(“%p”, &ch);

  6. 0x3A15 0x2000 chPtr Pointers A variable which can store the memory address of another variable. 0x1FFE 0x1FFF 0x2000 0x2001 0x2002 etc. ‘B’ ch

  7. Pointers • A pointer is a variable. • Contains a memory address. • Points to a specific data type. • Pointer variables are usually named varPtr.

  8. Example: char* cPtr; cPtr: 0x2004 Can store an address of variables of type char • We say cPtris a pointer to char.

  9. c: cPtr: 0x2000 A 0x2004 Pointers and the & Operator Example: char c = ’A’; char *cPtr; cPtr = &c; Assigns the address ofc to cPtr. 0x2000

  10. We can have pointers to any data type. • The * can be anywhere between the type and the variable. Example: int* numPtr; float* xPtr; Example: int *numPtr; float * xPtr; Notes on Pointers

  11. You can assign the address of a variable to a “compatible” pointer using the &operator. int aNumber; int *numPtr; numPtr = &aNumber; Example: • You can print the address stored in a pointer using the %pconversion specifier. Example: printf(“%p”, numPtr); Notes on Pointers (cont.)

  12. Notes on Pointers (cont.) Beware of pointers which are not initialized! int *numPtr; ??? numPtr

  13. Notes on Pointers (cont.) • When declaring a pointer, it is a good idea to always initialize it toNULL (a special pointer constant). int *numPtr = NULL; NULL numPtr

  14. The *Operator • Allows pointers to access to variables they point to. • Also known as “dereferencing operator.” • Should not be confused with the * in the pointer declaration.

  15. cPtr: c: 0x2000 0x2004 A Pointers and the  Operator Example: char c = ’A’; char *cPtr = NULL; Changes the value of the variable which cPtr points to. cPtr = &c; *cPtr = ’B’; B 0x2000

  16. Easy Steps to Pointers int num; char ch = ‘A’; float x; • Step 1: Declare the variable to be pointed to. num: ch: ‘A’ x:

  17. Easy Steps to Pointers int num; char ch = ‘A’; float x; • Step 2: Declare the pointer variable. numPtr: int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; chPtr: xPtr: num: ch: ‘A’ x:

  18. Easy Steps to Pointers int num; char ch = ‘A’; float x; • Step 3: Assign address of variable to pointer. numPtr: addr of num int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; chPtr: addr of ch xPtr: addr of x numPtr = # chPtr = &ch; xPtr = &x; num: ch: ‘A’ A pointer’s type has to correspond to the type of the variable it points to. x:

  19. Easy Steps to Pointers int num; char ch = ‘A’; float x; • Step 4: De-reference the pointers. numPtr: addr of num int* numPtr = NULL; char *chPtr = NULL; float * xPtr = NULL; chPtr: addr of ch xPtr: addr of x numPtr = # chPtr = &ch; xPtr = &x; num: 65 *xPtr = 0.25; *numPtr = *chPtr; ch: ‘A’ x: 0.25

  20. Pointers and Function Parameters • Example: Function to swap the values of two variables: swap x: 1 y: 2 x: 2 y: 1

  21. #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } Bad swap

  22. #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } Bad swap x: 1 y: 2

  23. #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } Bad swap tmp: a: 1 b: 2 x: 1 y: 2

  24. #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } Bad swap tmp: 1 a: 1 b: 2 x: 1 y: 2

  25. #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } Bad swap tmp: 1 a: 2 b: 2 x: 1 y: 2

  26. #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } Bad swap tmp: 1 a: 2 b: 1 x: 1 y: 2

  27. #include <stdio.h> void swap1(int a, int b) { int tmp; tmp = a; a = b; b = tmp; } main() { int x = 1, y = 2; swap1(x, y); printf(“%d %d\n”, x, y); } Bad swap tmp: 1 a: 2 b: 1 x: 1 y: 2

  28. #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } Good swap

  29. #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } Good swap x: 1 y: 2

  30. #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } Good swap tmp: a: addr of x b: addr of y x: 1 y: 2

  31. #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } Good swap tmp: 1 a: addr of x b: addr of y x: 1 y: 2

  32. #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } Good swap tmp: 1 a: addr of x b: addr of y x: 2 y: 2

  33. #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } Good swap tmp: 1 a: addr of x b: addr of y x: 2 y: 1

  34. #include <stdio.h> void swap2(int* a, int* b) { int tmp; tmp = *a; *a = *b; *b = tmp; } main() { int x = 1, y = 2; swap2(&x, &y); printf(“%d %d\n”, x, y); } Good swap x: 2 y: 1

  35. scanfdemystified. char ch; int numx; float numy; scanf(“%c %d %f”, &ch, &numx, &numy); Pointers and Function Arguments • Change the value of an actual parameter variable.

More Related