SlideServe
Menu
Préférences
© SlideServe · v2026

Understanding Constant Pointers and Pointers to Constants in C++

balin avatar
10 diapositives · 253 Views · Jul 14, 2014

This article explores the nuances of constant pointers and pointers to constants in C++. It clarifies three key situations: a pointer to a constant, a constant pointer, and a constant pointer to a constant. Each situation is explained with code examples demonstrating how they work in practice, including the limitations and behaviors associated with these types of pointers. Readers will gain a clear understanding of how to manage memory and enforce constancy in their C++ programs.

Understanding Constant Pointers and Pointers to Constants in C++ - slide 1
01 / 10Understanding Constant Pointers and Pointers to Constants in C++
1 / 17

Télécharger la présentation

Understanding Constant Pointers and Pointers to Constants in C++
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. Constant Pointers and Pointers to Constants • It is possible to distinguish three situations arising from using const with pointers and the things to which they point: • A pointer to a const. • Here, what’s pointed to cannot be modified, but we can set the pointer to point to something else: const char* pstring= “This text cannot be \ changed!”; tMyn

  2. #include "stdafx.h" #include <iostream> using namespace System; using namespace std; int main(array<System::String ^> ^args) { const int integer1=20; const int* pInt=&integer1; cout<<"Variable integer1 is a constant and cannot \ be changed.. " << endl<<"and it's value is now and always: " <<*pInt<<"."<<endl; const int integer2=40; pInt=&integer2; You couldn’t store the address of integer1 in a non-const pointer!! tMyn

  3. cout<<"But it is still possible to set the pointer to \ point to something else!" <<endl<<"Variable integer2 is a constant and \ cannot be changed.. " << endl<<"and it's value is now and always: " <<*pInt<<"."<<endl; return 0; } tMyn

  4. A constant pointer. • Here, the address stored in the pointer can’t be changed, so a pointer like this can only ever point to the address it is initialized with. • However, the contents of that address are not constant and can be changed. tMyn

  5. #include "stdafx.h" #include <iostream> using namespace System; using namespace std; int main(array<System::String ^> ^args) { int integer1=20; int* const pInt=&integer1; cout<<"Pointer variable pInt is const, so it can \ only ever point to variable integer1." <<endl<<"However, the contents of variable integer1 is \ not const!"<<endl; The pointer pInt can only point to a non-const variable of type int! tMyn

  6. integer1=30; cout<<"The value of the variable integer1 is this time " <<*pInt<<"."<<endl; return 0; } tMyn

  7. A constant pointer to a constant. • Here, both the address stored in the pointer and the thing pointed to have been declared as constant, so neither can be changed. tMyn

  8. #include "stdafx.h" #include <iostream> using namespace System; using namespace std; int main(array<System::String ^> ^args) { const int integer1=20; const int* const pInt=&integer1; cout<<"Pointer variable pInt is a constant \ pointer to a constant, "<<endl <<"so it is not possible to change \ what pInt points to."<<endl tMyn

  9. <<"It is neither possible to change the value \ at the address it contains."<<endl <<"The value of the variable integer1 is now \ and always "<<*pInt<<"."<<endl; return 0; } tMyn

  10. Naturally, this behaviour isn’t confined to the int types we have been dealing with so far. • This discussion applies to pointers of any type. tMyn