lecture 11 Flashcards
- Each variable is stored at a ………….. and we use the ……………..to get it
- unique address , we address operator & to get address of a variable
for exmple :-
int num = -90;
cout «_space; &num ; will print out the address of this
variable in hexadecimal
Pointer variable ( pointer) it’s a…………….
its called a pointer because iit points to the data
variable that holds an address
arrays and reference variables are like pointers but the difference is that arays and referencevariables are done automatically but pointers are manually
..
- A pointer that contains the address 0 is called a …………..
- The null pointer is represented by the literal value ………….. or ………
- null pointer
- nullptr (or NULL)
int *intptr;
is a pointer of type int , meaning that it can hold the adress of an integer variable
intptr = #
assigning the pointer to point to the address of variable num
what is the output of each
int x = 25;
int *ptx = &x;
cout «_space;ptx ;
cout «_space;*ptx;
*ptx = 100 ;
cout «_space;x ;
first one prints address of x i hexadecimal
second one prints value of x 25
third one prints value of x after assigning 100 to the address pointed to by ptx
int a; //Integer
int* p; //Pointer
int** q; //Pointer to Pointer
a = 58;
p = &a;
q = &p;
cout «_space;a ;
cout «_space;p ;
cout «_space;*p ;
cout «_space;q ;
cout «_space;*q ;
cout «_space;**q ;
cout «_space;&q ;
1) 58
2) address of a
3) 58
4) address of pointer p which is same as value in pointer q
5) value stored in pointer q ie: address of pointer p
6) value stored in address of pointer p = 58
7) address of pointer q
int array[ ] = { 4, 5 , 6} ;
cout «_space;* array ; // value stored in pointer array
4 because Array name can be used as a pointer constant as it poits to the value of the first element
int array[ ] = { 4, 5 , 6} ;
int * arrayptr = array;
cout «_space; arrayptr [1] ;
5 because Pointer can be used as an array name
int vals[ ] = { 4, 7, 11} ;
int *valptr ;
valptr = vals ;
cout «_space;*(valptr+1) ;
cout «_space;*(valptr+2) ;
- value stored in address ofsecond element = 7
- value stored in address of third element = 11
we can access the value stored in a specific index of an array in 4 ways :
RULE : array [ i ] is equivalent to *(array + i )
int values [ ] = { 0 , 1 , 17} ;
int *ptv ;
ptv = values ;
cout «_space;values [2] ;
cout «_space;ptv [2] ;
cout «_space;*(values + 2) ;
cout «_space;*(ptv + 2) ;
operations on pointers :
int vals[ ] = { 4 , 7 , 11 };
int *valptr = vals ;
cout «_space;valptr ++ ;
cout «_space;valptr – ;
cout «_space;* (valptr + 2) ;
cout «_space;valptr = vals ;
cout «_space;valptr += 2 ;
cout «_space;valptr–val ;
1) increments the pointer by 1 to point at 7
2) decrements the pointer by 1 to point at 4
3) value stored at index 2 in the array = 11
4) value stored in index 0 = 4
5) value stored at index 2 = 11
6) value stored at index that 1st pointer valptr points to and 2nd pointer val points to
if (ptr1 == ptr2) // compares addresses
if (*ptr1 == *ptr2) // compares contents
…………..operator is used to allocate memory dynamically while the program isrunning
and returns ……….
“new”
address
1) automatic (static ) memoy allocation :
- any variable that is declared in the compile time is stored in the stac memory
- is only availble within the scope during compile time
- the allocation and deallocation is done automatically by the compiler
- stack memory is named stac because first in last out (meaning first declared vaiable or function is the last one deleted)
- stack memory is limited and can easiily runout
2) dynamic (heap ) memory allocation :
- is in the heap memory that is done by the user