C++ CH 4 Pointer
C++ CH 4 Pointer
Pointer
Pointer?
pointer is a variable that holds the address of another variable.
r example, if one variable contains the address of another variable, the first variable is said to point to
int num=10;
cond. int *ptr;
Ptr=#
pointer of type integer can hold the address of a variable of type integer.
pointer of character type can hold the address of a variable of character type.
e base type of the pointer defines what type of variables the pointer can point to.
Addresses(&)
• To understand C++ pointers, you must understand how computers store data.
• When you create a variable in your C++ program, it is assigned some space
the computer memory. The value of this variable is stored in the assigned
location.
• To know the location in the computer memory where the data is stored, C++
provides the & (reference) operator.
x number ch
Example
The address of a variable can be obtained by using the address-of operator &.
int x;
2000 2002 2006
float number;
x number ch
char ch;
5
• Reference operator (&) and Deference operator (*)
• The reference operator (&) returns the variable’s address.
• & is the reference operator and can be read as "address of"
• The dereference operator (*) helps us to get the value that has been stored
in a memory address.
• * is the dereference operator and can be read as "value pointed by"
• For example: num 20 Num==20
int num=20; &num==x1780
x1780 Pt==x1780
int *pt;
*pt==20
pt=&num
Pointer declaration
• Like other variable, you must declare a pointer before you can work with it.
• type is the pointer's base type; it must be a valid C++ type. This type is not the
type of the pointer itself, but the type of the data the pointer points to.
8
#include<iostream> output
using namespace std;
int main() the address of num is : 0x6bfec8
{ the pointer variable pt holds : 0x6bfec8
the value of *pt is : 30
int num=30;
int * pt;
pt=#
cout<<" the address of num is : "<<&num<<endl;
cout<<" the pointer variable pt holds : "<<pt<<endl;
cout<<" the value of *pt is : "<<*pt;
}
Pointer initialization
Pointers can be initialized to point to specific locations at the very moment they are
defined: int myvar;
int myvar; int * myptr;
int * myptr = &myvar; myptr = &myvar;
When pointers are initialized, what is initialized is the address they point to
(i.e., myptr), never the value being pointed (i.e., *myptr). Therefore, the code above
shall not be confused with:
int myvar;
int * myptr;
*myptr = &myvar; //not valid code.
Pointers can be initialized either to the address of a variable or to the value of
another pointer
int myvar;
int *foo = &myvar;
int *bar = foo;
Pointer has a Type
A pointer can only hold an address of the declared type; it cannot hold
an address of a different type.
Pointers and Arrays
• Pointers can hold not only the address of a single variable, but also it can
also store the address of cells of an array.
• p and numbers would be equivalent and would have the same properties.
• the only difference is that we could change the value of pointer p by another one,
whereas numbers will always point to the first of the 20 elements of type int with
which it was defined.
• int *x[10];
• To assign the address of an integer variable called var to the third element of the
pointer array, write x[2] = &var;
• *x[2]
Pointer Arithmetic
• There are only two arithmetic operations that you may use on pointers:
addition and subtraction.
and that we know that they point to memory locations 1000, 2000 and 3000 respectively.
• To understand what occurs in pointer arithmetic, let p1 be an integer pointer with a
current value of 2000. Also, assume integers are 4 bytes long. After the expression
p1++;
• The reason for this is that each time p1 is incremented, it will point to the next integer.
• The same is true of decrements. For example, assuming that p1 has the value 2000,
The value of each variable is written inside each cell; under the cells are their respective addresses in
memory.
• void (*fun_ptr)(int);
• fun_ptr = &fun;
• We can think of function pointers like normal C++ functions. Where void is the function’s return
type. *fun_ptr is a pointer to a function that takes one int argument. It’s as if we are declaring a
function called *fun_ptr which takes int and returns void.
• Address of a function
• To get the address of a function, we must first state the function’s name. There is no need for us to
call the function.
#include <iostream>
int main()
return 0;