SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Pointers in C++: Basics, Syntax, and Usage

anthony-nolan avatar
13 diapositives · 282 Views · Nov 01, 2014

This guide provides an overview of pointers in C++, covering their definition as objects representing memory addresses, their usefulness for dynamic memory allocation, and their role in handling flexible-sized arrays. It also includes fundamental syntax examples, including initialization of pointers, dereferencing, and handling null addresses. Additionally, explore traditional pointer usage in C++ for functions like indirect swapping and passing by reference, making it essential for efficient memory management and data manipulation.

Understanding Pointers in C++: Basics, Syntax, and Usage - slide 1
1 / 13

Télécharger la présentation

Understanding Pointers in C++: Basics, Syntax, and Usage
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  1. Programming Pointers

  2. Pointers • Pointers are objects whose values are the locations of other objects • Pointers are memory addresses

  3. Pointers • Usefulness • Necessary for dynamic objects • Objects whose memory is allocated during program execution. • Dynamic objects can survive after the function ends in which they were allocated. • Dynamic objects allow flexible-sized arrays

  4. Basics • Pointer • Object whose value represents the location (memory address) of another object • C++ has pointer types for every type of object • Pointers to int objects • Pointers to char objects • Pointers to user-defined objects (e.g., Temperature) • Even pointers to pointers • Pointers to pointers to int objects

  5. Syntax • Examples of uninitialized pointers int *iPtr; // iPtr is a pointer to an int char *s; // s is a pointer to a char Rational *rPtr; // rPtr is a pointer to a // Rational • Examples of initialized pointers int i = 1; char c = 'y'; int *ptr; // ptr is an int pointer ptr = &i; // ptr is assigned the address of i char *t; // t is a char pointer t = &c; // t is assigned the address of c Indicates pointer object “&i” means “the address of i”

  6. Memory Depiction i 1 int i = 1; char c = 'y'; int *ptr = &i; char *t; t = &c t points to c 'y' address of t a ptr character

  7. Address Operator • Another example: int i = 1; int j = 2; int *ptr; ptr = &i; // ptr points to location of i *ptr = 3; // contents of i are updated ptr = &j; // ptr points to location of j *ptr = 4; // contents of j are updated cout << i << " " << j << endl;

  8. Indirection Operator • An asterisk (“*”) has two uses for pointers • We have already seen that in a declaration an asterisk indicates that the object is a pointer. char *s; // s is of type pointer to char • In expressions, an asterisk before a pointer indicates that we want to dereference the pointer (“content of” operator ). int i = 1, j; int *ptr; // ptr is an int pointer ptr = &i; // ptr points to i j = *ptr + 1; // j is assigned 2 cout << *ptr << j << endl; // display "12"

  9. Null Address • 0 (NULL) is a pointer constant that represents the empty or null address • Indicates that pointer is not pointing to storage of a valid object • Cannot dereference a pointer (refer to the object pointed at by a pointer) whose value is null int *ptr; ptr = 0; cout << *ptr << endl; // invalid, ptr // does not point to // a valid int

  10. Member Indirection • Consider Rational r(4,3); Rational *rPtr = &r; • To select a member of r through indirection using rPtr, operator precedence requires we do thefollowing (*rPtr).Display(); • This looks strange, so C++ provides the indirect member selector operator -> rPtr->Display(); Calls member Display of the object to which rPtr points (r) Calls member Display of the object to which rPtr points (r)

  11. Traditional Pointer Usage in C void IndirectSwap(char *Ptr1, char *Ptr2) { char c = *Ptr1; *Ptr1 = *Ptr2; *Ptr2 = c; } int main() { char a = 'y'; char b = 'n'; IndirectSwap(&a, &b); cout << a << b << endl; return 0; }

  12. Pass by Reference in C++ void IndirectSwap(char& y, char& z) { char temp = y; y = z; z = temp; } int main() { char a = 'y'; char b = 'n'; IndirectSwap(a, b); cout << a << b << endl; return 0; }

  13. 0 1 2 ptr 3 4 Example int arr[5], *ptr, *ptr2, j; for (int i=0;i<5;i++) arr[i] = i; ptr = arr; cout << *(ptr+3) << endl; // 3 cout << *ptr << endl; // 0 ptr++; cout << *ptr << endl; // 1 ptr += 2; cout << *ptr << endl; // 3 j = *ptr * 10; cout << j << endl; // 30 ptr2 = ptr - 2; cout << *ptr2 << endl; // 1 cout << ptr + 1 << endl; // address arr arr+1 arr+2 arr+3 arr+4