17 Oktober 2018 - Pointer and Arrays

Pointer and Array

Pointer and array:
  • Pointer Definition 
  • Array Definition
  • Array Initialization 
  • Array
A. Pointer Definition
     Pointer is a variable that store the address of another variable

     Syntax :
   <type> *ptr_name;

     Two operators mostly used in pointer : * (content of) and & (address of)
     Example: 
     Initialize an integer pointer into a data variable: 
     int i, *ptr;
     ptr = &i;
     To assign a new value to the variable pointed by the pointer:
     *ptr = 5;  /* means i=5 */ 

B. Array Definition
     Data saved in a certain structure to be accessed as a group or individually. Some variables saved         using the same name distinguish by their index.

     Array characteristics:
  •      Homogenous
  •      All elements have similar data type
  •      Random Access
  •      Each element can be reached individually, does not have to be sequential
     Syntax:
type array_value [value_dim];

     Example :
int A[10];

     The definition consists of 4 components:
  • Type specified
  • Identifier (name of the array)
  • Operator index ([  ])
  • Dimensional value inside operator [ ]
C. Array Initialization
     Array can be initialized explicitly without dimensional value declaration
     Example: 
int B[ ]={1, 2, -4, 8};


Comments