1 / 36

Math 130 Introduction to Computing Functions and Introduction to Pointers Lecture # 10

B Smith: Required 2 lectures Fall04. Math 130 Introduction to Computing Functions and Introduction to Pointers Lecture # 10. Topics. Introduction to pointers Pointers as function parameters. ch:. ’A’. 0x2000. Memory Address of a Variable. char ch = ’A’;.

shauna
Télécharger la présentation

Math 130 Introduction to Computing Functions and Introduction to Pointers Lecture # 10

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. B Smith: Required 2 lectures Fall04 Math 130Introduction to ComputingFunctions and Introduction to PointersLecture # 10

  2. Topics • Introduction to pointers • Pointers as 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 ‘A’ ch

  7. Pointers • A pointer is a variable which… • 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 a character

  9. c: cPtr: A 0x2000 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 Example: int* numPtr; float* xPtr; • The * can be anywhere between the type and the variable 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 variables they point to • Also known as “dereferencing operator” • Should not be confused with the * in the pointer declaration

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

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

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

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

  19. Your Turn… • Write a fragment of C code that does the following: • Declares 3 integer variables called a, b, and c. • Declares 3 integer pointers p1, p2, and p3. • Assigns the values 34, 10, and –4 to a, b, and c • Initializes p1 with the addres of a and initializes p2 with the address of b • Points p3 to the same item pointed to by p2 • Prints out the contents pointed to by p1, p2 and p3

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  36. Lessons • Pointers are simply variables that contain an address • a pointer holds an address • a pointer holds an address • a pointer holds an address

More Related