What is a Pointer?
The basic definition of a pointer is a variable that stores an address. Pointers are used to store the adresses of other variables. Normally a variable contains a specific value. A pointer on the other hand contains the memory address of a variable which, in turn, contains a specific value.
Principle of Least Privilege - code should be granted only the amount of privilege and access needed to accomplish its task, but no more.
typeName* variableName;
int x, y, z; //declaration of three variables of type int int* p, q, r; //appears to declare three pointers to ints, but actually creates one pointer and two ints. int * p, * q, * r; //correct way to declare three pointers to ints on one line
int count; //declaration of variable count int* countPtr; //declaration of pointer countPtr
int* countPtr; int *countPtr; int * countPtr;
int y = 5; // declare variable y int* yPtr; // declare pointer variable yPtr yPtr = &y; // assign address of y to yPtr
int& count;
std::cout << *yPtr << endl; // prints the value of y just as, std::cout << y << endl; // prints the vlaue of y
cout << "The data value is " << *yPtr; // prints the value
int y = 5; // declare variable y int* yPtr; // declare pointer variable yPtr yPtr = &y; // assign address of y to yPtr cout << "The pointer is: " << yPtr; // prints the pointer cout << "The data value is " << *yPtr; // prints the value // Output // The pointer is: 1234 // actual output depends on address // The value is: 5
*yPtr = 9;
int* yPtr; yPtr = 0; --OR-- int* yPtr = 0;
if (yPtr != 0) // safe to dereference
cout << *yPtr;
int * xPtr, * yPtr; // two pointers to int xPtr = yPtr; // both point to the same location
int y = 5; // declare variable y int* yPtr; // declare pointer variable yPtr yPtr = &y; // assign address of y to yPtr
// Fig. 8.4: fig08_04.cpp
// Pointer operators & and *.
#include <iostream>
using namespace std;
int main()
{
int a; // a is an integer
int *aPtr; // aPtr is an int * which is a pointer to an integer
a = 7; // assigned 7 to a
aPtr = &a; // assign the address of a to aPtr
cout << "The address of a is " << &a
<< "\nThe value of aPtr is " << aPtr;
cout << "\n\nThe value of a is " << a
<< "\nThe value of *aPtr is " << *aPtr;
cout << "\n\nShowing that * and & are inverses of "
<< "each other.\n&*aPtr = " << &*aPtr
<< "\n*&aPtr = " << *&aPtr << endl;
} // end main
// Fig. 8.7: fig08_07.cpp
// Pass-by-reference with a pointer argument used to cube a
// variable's value
#include <iostream>
using namespace std;
void cubeByReference( int * ); // prototype
int main()
{
int number = 5;
cout << "The original value of number is " << number;
cubeByReference( &number ); // pass number address to cubeByReference
cout << "\nThe new value of number is " << number << endl;
} // end main
// calculate cube of *nPtr; modifies variable number in main
void cubeByReference( int *nPtr )
{
*nPtr = *nPtr * *nPtr * *nPtr; // cube *nPtr
} // end function cubeByReference
// Fig. 8.6: fig08_06.cpp
// Pass-by-value used to cube a variable
#include <iostream>
using namespace std;
int cubeByValue( int ); // prototype
int main()
{
int number = 5;
cout << "The original value of number is " << number;
number = cubeByValue( number ); // pass number by value to cubeByValue
cout << "\nThe new value of number is " << number << endl;
} // end main
// calculate and return cube of integer argument
int cubeByValue( int n )
{
return n * n * n; // cube local variable n and return result
} // end function cubeByValue
int* countPtr;
const int* countPtr;
int* const countPtr = &x; //const pointer must be initialized when declared
const int* const countPtr = &x; //const pointer must be initialized when declared