Pointers and Addresses in C

Pointers and Addresses in C
Slide Note
Embed
Share

Pointers and addresses play a crucial role in C programming. A pointer is a variable that holds the memory address of another variable. By utilizing pointers, you can directly access and manipulate memory addresses, making C a powerful and efficient programming language. This guide explores the concept of pointers, variables, addresses, pointer syntax, and dereferencing operators, providing insights into how they work in the context of C programming.

  • Pointers
  • Addresses
  • C Programming
  • Variables
  • Dereferencing

Uploaded on Feb 19, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Pointers COMP 11

  2. Variables and Addresses int x; x = 42; 42 a x y char y; y = 'a';

  3. Variables and Addresses int x; x = 42; 42 a x y 5678 1234 char y; y = 'a'; // & is the address of operator cout << "The address of x is "; cout << &x << endl; Variable address : where the variable is located in memory

  4. What is a Pointer? A pointer is a variable that contains the address of a variable. --Inventors of C

  5. What is a Pointer? A pointer is a variable that contains the address of a variable. --Inventors of C 42 a x y 5678 1234

  6. What is a Pointer? A pointer is a variable that contains the address of a variable. --Inventors of C 42 a x p y 5678 7385 1234

  7. What is a Pointer? A pointer is a variable that contains the address of a variable. --Inventors of C 1234 42 a x p y 5678 7385 1234

  8. Pointer Syntax 9 42 int w = 9, x = 42; w x 9021 5678

  9. Pointer Syntax 9 42 int w = 9, x = 42; int *p; /* "*p is an int" */ w x 9021 5678 ? p 1234

  10. Pointer Syntax 9 42 int w = 9, x = 42; int *p; /* "*p is an int" */ p = &x; /* p points to x */ w x 9021 5678 p 1234 When applied to a pointer, * is the dereferencing operator. If p is a pointer to location x, *p is the value at location x.

  11. Pointer Syntax 9 42 int w = 9, x = 42; int *p; /* "*p is an int" */ p = &x; /* p points to x */ cout << *p << endl; w x 9021 5678 p 1234 When applied to a pointer, * is the dereferencing operator. If p is a pointer to location x, *p is the value at location x.

  12. Pointer Syntax 9 42 int w = 9, x = 42; int *p; /* "*p is an int" */ w x 9021 5678 cout << *p << endl; ??? p 1234 When applied to a pointer, * is the dereferencing operator. If p is a pointer to location x, *p is the value at location x.

  13. Pointer Syntax 9 42 int w = 9, x = 42; int *p; /* "*p is an int" */ p = &x; /* p points to x */ w x 9021 5678 p 1234 When applied to a pointer, * is the dereferencing operator. If p is a pointer to location x, *p is the value at location x.

  14. Pointer Syntax 42 42 int w = 9, x = 42; int *p; /* "*p is an int" */ p = &x; /* p points to x */ w = *p; /* w now holds 42 */ w x 9021 5678 p 1234 When applied to a pointer, * is the dereferencing operator. If p is a pointer to location x, *p is the value at location x.

  15. Pointer Syntax 42 12 int w = 9, x = 42; int *p; /* "*p is an int" */ p = &x; /* p points to x */ w = *p; /* w now holds 42 */ *p = 12;/* x now holds 12 */ w x 9021 5678 p 1234 When applied to a pointer, * is the dereferencing operator. If p is a pointer to location x, *p is the value at location x.

  16. Pointer Syntax 42 12 int w = 9, x = 42; int *p; /* "*p is an int" */ p = &x; /* p points to x */ w = *p; /* w now holds 42 */ *p = 12;/* x now holds 12 */ cout << *p + 1 << endl; w x 9021 5678 p 1234 When applied to a pointer, * is the dereferencing operator. If p is a pointer to location x, *p is the value at location x.

  17. Check Your Understanding: Pointers What does this code print out? int x; int *p; int *q; x = 3; p = &x; cout << *p << , << x << endl; *p = *p - 1; cout << *p << , << x << endl; q = p; cout << *p << , << *q << endl; *q = *p - 1; cout << *p << , << *q << , << x << endl; p x q

  18. Check Your Understanding: Pointers What does this code print out? int x; int *p; int *q; x = 3; p = &x; cout << *p << , << x << endl; *p = *p - 1; cout << *p << , << x << endl; q = p; cout << *p << , << *q << endl; *q = *p - 1; cout << *p << , << *q << , << x << endl; p x 3, 3 3 q

  19. Check Your Understanding: Pointers What does this code print out? int x; int *p; int *q; x = 3; p = &x; cout << *p << , << x << endl; *p = *p - 1; cout << *p << , << x << endl; q = p; cout << *p << , << *q << endl; *q = *p - 1; cout << *p << , << *q << , << x << endl; p x 3, 3 3 q

  20. Check Your Understanding: Pointers What does this code print out? int x; int *p; int *q; x = 3; p = &x; cout << *p << , << x << endl; *p = *p - 1; cout << *p << , << x << endl; q = p; cout << *p << , << *q << endl; *q = *p - 1; cout << *p << , << *q << , << x << endl; p x 3, 3 2 2, 2 q

  21. Check Your Understanding: Pointers What does this code print out? int x; int *p; int *q; x = 3; p = &x; cout << *p << , << x << endl; *p = *p - 1; cout << *p << , << x << endl; q = p; cout << *p << , << *q << endl; *q = *p - 1; cout << *p << , << *q << , << x << endl; p x 3, 3 2 2, 2 q

  22. Check Your Understanding: Pointers What does this code print out? int x; int *p; int *q; x = 3; p = &x; cout << *p << , << x << endl; *p = *p - 1; cout << *p << , << x << endl; q = p; cout << *p << , << *q << endl; *q = *p - 1; cout << *p << , << *q << , << x << endl; p x 3, 3 2 2, 2 q 2, 2

  23. Check Your Understanding: Pointers What does this code print out? int x; int *p; int *q; x = 3; p = &x; cout << *p << , << x << endl; *p = *p - 1; cout << *p << , << x << endl; q = p; cout << *p << , << *q << endl; *q = *p - 1; cout << *p << , << *q << , << x << endl; p x 3, 3 1 2, 2 q 2, 2 1, 1, 1

  24. Whats the point? void swap(int x, int y) { int temp; temp = y; y = x; x = temp; } int main() { int a = 7, b = 14; cout << a << << b << endl; // This should print 7 14 swap(a, b); cout << a << << b << endl; // This should print 14 7 }

  25. Whats the point? x void swap(int *x, int *y) { int temp; temp = *y; *y = *x; *x = temp; } a3b5 y ff24 14 temp 9e2a int main() { int a = 7, b = 14; cout << a << << b << endl; swap(&a, &b); cout << a << << b << endl; } 7 14 a 1234 b 5678

  26. Whats the point? x void swap(int *x, int *y) { int temp; temp = *y; *y = *x; *x = temp; } a3b5 y ff24 14 temp 9e2a int main() { int a = 7, b = 14; cout << a << << b << endl; swap(&a, &b); cout << a << << b << endl; } 7 7 a 1234 b 5678

  27. Whats the point? x void swap(int *x, int *y) { int temp; temp = *y; *y = *x; *x = temp; } a3b5 y ff24 14 temp 9e2a int main() { int a = 7, b = 14; cout << a << << b << endl; swap(&a, &b); cout << a << << b << endl; } 14 7 a 1234 b 5678

  28. Whats the point? void swap(int *x, int *y) { int temp; temp = *y; *y = *x; *x = temp; } Pointers enable pass-by-reference: A function can modify variables outside its scope via pointers to those variables. int main() { int a = 7, b = 14; cout << a << << b << endl; swap(&a, &b); cout << a << << b << endl; }

  29. Exercise: Pointers and Functions What does this code print out? void f(int n, int *x) { *x = 1; for (int i = 2; i <= n; i++) { *x = (*x) * i; } } int main() { int num1 = 4; int num2 = 10; f(num1, &num2); cout << num1 << << num2 << endl; return 0; } num1 num2

  30. Exercise: Pointers and Functions What does this code print out? void f(int n, int *x) { *x = 1; for (int i = 2; i <= n; i++) { *x = (*x) * i; } } int main() { int num1 = 4; int num2 = 10; f(num1, &num2); cout << num1 << << num2 << endl; return 0; } n x 4 num1 4 num2 10

  31. Exercise: Pointers and Functions What does this code print out? void f(int n, int *x) { *x = 1; for (int i = 2; i <= n; i++) { *x = (*x) * i; } } int main() { int num1 = 4; int num2 = 10; f(num1, &num2); cout << num1 << << num2 << endl; return 0; } n x 4 num1 4 num2 10

  32. Exercise: Pointers and Functions What does this code print out? void f(int n, int *x) { *x = 1; for (int i = 2; i <= n; i++) { *x = (*x) * i; } } int main() { int num1 = 4; int num2 = 10; f(num1, &num2); cout << num1 << << num2 << endl; return 0; } n x 4 num1 4 num2 1

  33. Exercise: Pointers and Functions What does this code print out? void f(int n, int *x) { *x = 1; for (int i = 2; i <= n; i++) { *x = (*x) * i; } } int main() { int num1 = 4; int num2 = 10; f(num1, &num2); cout << num1 << << num2 << endl; return 0; } i n x 2 4 num1 4 num2 1

  34. Exercise: Pointers and Functions What does this code print out? void f(int n, int *x) { *x = 1; for (int i = 2; i <= n; i++) { *x = (*x) * i; } } int main() { int num1 = 4; int num2 = 10; f(num1, &num2); cout << num1 << << num2 << endl; return 0; } i n x 2 4 num1 4 num2 2

  35. Exercise: Pointers and Functions What does this code print out? void f(int n, int *x) { *x = 1; for (int i = 2; i <= n; i++) { *x = (*x) * i; } } int main() { int num1 = 4; int num2 = 10; f(num1, &num2); cout << num1 << << num2 << endl; return 0; } i n x 3 4 num1 4 num2 2

  36. Exercise: Pointers and Functions What does this code print out? void f(int n, int *x) { *x = 1; for (int i = 2; i <= n; i++) { *x = (*x) * i; } } int main() { int num1 = 4; int num2 = 10; f(num1, &num2); cout << num1 << << num2 << endl; return 0; } i n x 3 4 num1 4 num2 6

  37. Exercise: Pointers and Functions What does this code print out? void f(int n, int *x) { *x = 1; for (int i = 2; i <= n; i++) { *x = (*x) * i; } } int main() { int num1 = 4; int num2 = 10; f(num1, &num2); cout << num1 << << num2 << endl; return 0; } i n x 4 4 num1 4 num2 6

  38. Exercise: Pointers and Functions What does this code print out? void f(int n, int *x) { *x = 1; for (int i = 2; i <= n; i++) { *x = (*x) * i; } } int main() { int num1 = 4; int num2 = 10; f(num1, &num2); cout << num1 << << num2 << endl; return 0; } i n x 4 4 num1 4 num2 24

  39. Exercise: Pointers and Functions What does this code print out? void f(int n, int *x) { *x = 1; for (int i = 2; i <= n; i++) { *x = (*x) * i; } } int main() { int num1 = 4; int num2 = 10; f(num1, &num2); cout << num1 << << num2 << endl; return 0; } num1 4 num2 24

  40. Pointers, Arrays, and Pointer Arithmetic arr: int arr[5]; arr[0] arr[1] arr[4]

  41. Pointers, Arrays, and Pointer Arithmetic arr: int arr[5]; int *p = &arr[0]; arr[0] arr[1] arr[4] p:

  42. Pointers, Arrays, and Pointer Arithmetic arr: int arr[5]; int *p = &arr[0]; arr[0] arr[1] arr[4] p: p+1: p+4: Given a pointer p that points to array element a[n], the expression p + i is a pointer to a[n + i].

  43. Pointers, Arrays, and Pointer Arithmetic arr: 12 int arr[5]; int *p = &arr[0]; *(p + 1) = 12; arr[0] arr[1] arr[4] p:

  44. Pointers, Arrays, and Pointer Arithmetic arr: 12 int arr[5]; int *p = &arr[0]; *(p + 1) = 12; p = p + 1; arr[0] arr[1] arr[4] p:

  45. Pointers, Arrays, and Pointer Arithmetic arr: 12 int arr[5]; int *p = arr; //arr == &arr[0] *(p + 1) = 12; p = p + 1; arr[0] arr[1] arr[4] p:

  46. Pointers, Arrays, and Pointer Arithmetic arr: 12 10 int arr[5]; int *p = arr; *(p + 1) = 12; p = p + 1; arr[3] = 10; arr[0] arr[1] arr[4] p:

  47. Pointers, Arrays, and Pointer Arithmetic arr: 12 10 int arr[5]; int *p = arr; *(p + 1) = 12; p = p + 1; *(arr + 3) = 10; arr[0] arr[1] arr[4] p:

  48. Pointers, Arrays, and Pointer Arithmetic arr: 12 10 int arr[5]; int *p = arr; p[1] = 12; p = p + 1; *(arr + 3) = 10; arr[0] arr[1] arr[4] p:

  49. Pointers, Arrays, and Pointer Arithmetic If arr is an array, then arr[i] is equivalent to *(arr + i). If p is a pointer, then *(p + i) is equivalent to p[i]. If arr is an array, then writing arr is equivalent to writing &arr[0]. In other words, the name of an array is a synonym for the address of the zeroth element of the array.

  50. Pointers, Arrays, and Pointer Arithmetic int a[10]; int *p; p = a; //OK p++; a = p; //NOPE a++; //Sure //What are you thinking?

More Related Content