Senin, 23 November 2015

Pointers and reference in C

Pointers and Reference in C

     Definition of Pointers:
  • is a data type that store the addres of another variable. but you must declare a pointer before you using it to store any variable address.
  • you can declare a pointer using asterisk (*). example: int *a. the asterisk function is used to designate a variable as a pointer.
     Step to use Pointers?
  • create and define a pointer variable. (ex. int *x;).
  • create another variable that you want to store to pointer. (ex. int y = 10;).
  • access the value at the address in the pointer variable. (ex. x = &y).
  • that means that now variable x contains the address of variable y.
     to use pointers in another way?
  • define a variable that contains the address. (ex. int x = 10;)
  • and then create a pointer variable but point them into the variable that contains the address.  (ex. int *y = &x;)
    The example of to use pointers?

     #include <stdio.h>
     int main ()
     {
     int  x = 10;                         // create a variable
     int *y;                                // pointer variable declaration
     y = &x;                              // store address of x into y

     printf("address of x variable: %x\n",x);
     printf("address store of x into y pointer variable: %x\n",y);

     //access the value of the store address into y using pointer.
     printf("the value of y: %d\n",*y);
     
     return 0;
     }

     passing by pointers and the relation with function?
     #include <stdio.h>

     //function declaration
     int passing(int *x) {
     *x = 10;
     }

     int main ()
     {
     int y = 1;                             // x points to y
     passing(&y);                      // pass address of y to x
     printf("&d\n",y);                // now it will print 10 instead of 1
     return 0;
     }

     The relation of pointer with array?
     #include <stdio.h>
     int main ()
     {
     char c[4];
     int i;
     for (i=0;i<4;i++) {
     printf("Address of c[%d]=%x\n",i,&c[i]);
     }
     return 0;
     }

     How to pass pointer in array?
     int arr[5] = {1,2,3,4,5};
     int *x;
     x = &arr[1];                           // now it will point to arr[1]
     *x = 6;                                   //now arr[1] = 2 become x[1] = 6... {1,6,3,4,5}
     x+= 1;                                   //now move x to next address. start from arr[1].
     x[0] = 12;                              //now the result is {1,6,12,4,5}
     x[1] = 13;                              //now the result is {1,6,12,13,5}
     now you can access the data. (ex.you want to access 13, so you can call "arr[3]")

     References?
     references or alias is to make another name of a variable. example :
     int x = 10;
     int &y = x;                              // now x and y is the same variable
     y = 5;                                      //y=5 and the value of x also will be changed. x=5.

Tidak ada komentar:

Posting Komentar