Friday, 2 December 2011

Pointers

1. A pointer is a variable which contains the address of variable
You can't assign the address of a short int to a long int

Declare:
int *pt  ;                                                            int *pt = (int*)malloc(sizeof(int) );
int a =10 ;                                                        
 pt = &a;
+ *p -> value of a
+p - >  address of a
+&a -> address of a
+&p -> address of pt
#include <stdio.h>
#include <stdlib.h>
void func0(int i)
{
    i =1;
}
void func1(int *p)
{
    *p = 1;
}
void func2(int *p)
{
    p = (int*)malloc(sizeof(int));
    *p = 1;
}
void func3(int **p)
{
    *p = (int*)malloc(sizeof(int));
    **p = 1;
}
int main()
{
    int i = 0;
    func0(i);
    printf("function0 :%d \n",i);//0
    int *p =(int*)malloc(sizeof(int));
    *p = 0;
    func1(p);
    printf("function1 %d \n",*p);//1
    *p = 0;
    func2(p);
    printf("function2 %d \n",*p);//0
    *p = 0;
    func3(&p);
    printf("function3 %d \n",*p);//1
    return 0;
}



2 . Array and functions
+ value of variables  don't change  before Break out of function  ?
+ what do It return variables  ?
//-------------------------------------------------------------
3.Pointers  and  arrays
int a[10] ;
int *p = &a[0]; 
or int *p = &a;
//-----------------------------------------------------------------------------------
//---------------------------------------------------------------------
string 
The size of pointers 
The size of a pointer is dependent upon the architecture of the computer — a 32-bit computer uses 32-bit memory addresses — consequently, a pointer on a 32-bit machine is 32 bits (4 bytes). On a 64-bit machine, a pointer would be 64 bits (8 bytes). Note that this is true regardless of what is being pointed to:
1
2
3
4
5
6
7
8
9
10
11
char *pchValue; // chars are 1 byte
int *pnValue; // ints are usually 4 bytes
struct Something
{
    int nX, nY, nZ;
};
Something *psValue; // Something is probably 12 bytes
cout << sizeof(pchValue) << endl; // prints 4
cout << sizeof(pnValue) << endl; // prints 4
cout << sizeof(psValue) << endl; // prints 4
As you can see, the size of the pointer is always the same. This is because a pointer is just a memory address, and the number of bits needed to access a memory address on a given machine is always constant. 
NOTE
*&a = a ;//int   a
int a ; 
int *p ;
p = &a;
*&p = p  ;//which store address of integer i
&*p = &*(&i) //since p = &i
= &(*&i)
= &i
-Within the scope of any variable, value of variable may change but its address will never change in any modification of variable
-

int a[10];                                                   char str[10];
int *pt   ;                                                   char   *ptr ;
pt = a;// pt = &a[0]                                    ptr  = str;// ptr = &str[0];
int  *a[10];                                                int  (*pt)[4];
4.Arrays of pointers
int *a[10];

5. pointers to pointers
int **ptr_to_ptr;

int **ipp ;
int i = 5, j = 6; k = 7;int *ip1 = &i, *ip2 = &j;
when ipp = &ip1;

when *ipp = ip2

when *ipp = &k;

//------------------------------------------------------------------------------------------------------



6. Multidimensional arrays and pointers
*Multi arrays
int a[][];
address of multi- arrays
multi <->multi[0] <->&multi[0][0];

size of multi-arrays

*pointers
- mutli is pointer to mutli[0]
-mutli[0] is  pointer  to mutli[0][0]
=>mutli is pointer to pointer ( pointer const)
- value of multi[0][0]
+ mutli[0][0]
+*mutli[0]
+**multi
*(*(multi + row) + col) and
multi[row][col] 
exam:
int mutli[2][4];
int   (*ptr)[4];
ptr = mutli;
void func(int a[][max], int n , int m);
void func( int (*a)[] , int n , int m);
void  func(int **a , int  n , int m ) ;

//---------------------------------------------------------------------------------------------------------

//---------------------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------------
7.Pointers to a Function
form: type (*ptr)( type , type);
exam:
int  (*func1)(int x );
void  (*func2)(double x ,  double y);
char  ( *func3)(char *p[]);
void  ( *func4)();
//-----------------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------------

Note :char (*func)(); >< char *func()
            int  *a[10]   ><   int  (*pt)[4];

To declare a function ( func())  that value return pointer type : type *func();
8. Callback function 
+                https://en.wikipedia.org/


                                                                                                                                Thong LT 



No comments:

Post a Comment