Review: the short-hand operator ->

  • Recall the short-hand operator ->: ( click here)

       If  p  points to a struct  typed variable, then:
      
              p->member    ≡    (*p).member         
      

    The -> operator replaces the two (2) operators * and . for convenience

  • We will now learned another short-hand operator called the "array subscripting" operator:

          [  ]               
      

    See: click here

The short-hand operator [ ]

  • The [   ] operator is a binary operator

  • The [   ] operator must be applied as follows:

           p [ i ]        
      
         where:
      
            p is a  reference typed expression/variable 
            i is an integer   typed expression/variable
      

  • The meaning of the [  ] expression is a short hand for:

          p [ i ]  <===>    *(p + i)             
      

Application of the [ ] operator: accessing a (dynamic) array

Consider the previous example on accessing a (dynamic) array:

 #include <stdio.h>
 #include <stdlib.h>

 int main( int argc, char *argv[] )
 { 
     int *A; 
     int i;

     A = malloc( 10*sizeof(int) ); // Create a dynamic array 

     for ( i = 0; i < 10; i++ )
        *(A + i) = i*i;   // Assigns i^2 to A[i] 

     for ( i = 0; i < 10; i++ )
        printf("%d\n", *(A + i) );
 }
   

 

Application of the [ ] operator: accessing a (dynamic) array

We can re-write the example using the [ ] operator as follows:

 #include <stdio.h>
 #include <stdlib.h>

 int main( int argc, char *argv[] )
 { 
     int *A; 
     int i;

     A = malloc( 10*sizeof(int) ); // Create a dynamic array 

     for ( i = 0; i < 10; i++ )
          A[i]   = i*i;   // Assigns i^2 to A[i] 

     for ( i = 0; i < 10; i++ )
        printf("%d\n",   A[i]   );
 }
   

Now the dynamic array looks like an "ordinary" array !!!

Making an alias for a static array

Consider a static array B[ ]:

 #include <stdio.h>
 #include <stdlib.h>

 int main( int argc, char *argv[] )
 { 
     int B[5] = {3, 5, 1, 2, 4};

     int i;

     for ( i = 0; i < 5; i++ )
        printf("%d\n", B[i] );








 }  

Making an alias for a static array

Let's store the base address of array B[ ] in the reference variable A:

 #include <stdio.h>
 #include <stdlib.h>

 int main( int argc, char *argv[] )
 { 
     int B[5] = {3, 5, 1, 2, 4};
     int *A;
     int i;

     for ( i = 0; i < 5; i++ )
        printf("%d\n", B[i] );

     A = &B[0]; // A has the base address of array B[ ]






 }  

Making an alias for a static array

Result:   A[i] is an alias for B[i] for any value i:

 #include <stdio.h>
 #include <stdlib.h>

 int main( int argc, char *argv[] )
 { 
     int B[5] = {3, 5, 1, 2, 4};
     int *A;
     int i;

     for ( i = 0; i < 5; i++ )
        printf("%d\n", B[i] );

     A = &B[0]; // A has the base address of array B[ ]

     for ( i = 0; i < 5; i++ )
        A[i] = i*i;         // A[i] ≡ B[i] !! 

     for ( i = 0; i < 5; i++ )
        printf("%d\n", B[i] );
 } 

The name of an array in C
 

  • When we define an array using:

         int B[10];       
      

    the name of the array is B

  • In the C language, the expression B (= the name of an array) is equal to:

            B&B[0]         

    I.e.: the expression B is equal to the base address of the array B[ ]

    And the data type of the expression B is reference to int.

Making an alias for a static array - REVISITED

In the previous example, instead of &B[0], we can use   B :

 #include <stdio.h>
 #include <stdlib.h>

 int main( int argc, char *argv[] )
 { 
     int B[5] = {3, 5, 1, 2, 4};
     int *A;
     int i;

     for ( i = 0; i < 5; i++ )
        printf("%d\n", B[i] );

     A = &B[0]; // A has the base address of array B[ ]

     for ( i = 0; i < 5; i++ )
        A[i] = i*i;         // A[i] ≡ B[i] !! 

     for ( i = 0; i < 5; i++ )
        printf("%d\n", B[i] );
 } 

Making an alias for a static array - REVISITED

Resulting program:

 #include <stdio.h>
 #include <stdlib.h>

 int main( int argc, char *argv[] )
 { 
     int B[5] = {3, 5, 1, 2, 4};
     int *A;
     int i;

     for ( i = 0; i < 5; i++ )
        printf("%d\n", B[i] );

     A =  B   ; // A has the base address of array B[ ]

     for ( i = 0; i < 5; i++ )
        A[i] = i*i;         // A[i] ≡ B[i] !! 

     for ( i = 0; i < 5; i++ )
        printf("%d\n", B[i] );
 } 

Another way to define functions with array parameters

Recall how we defined functions with array as parameter: ( click here )

  #include <stdio.h>

  int sumArray( int A[], int n /*array length*/ )
  {
     int i, s;

     s = 0;
     for (i = 0; i < n; i++)
        s += A[i];
     return(s);
  }

  int main(int argc, char *argv[])
  {
     int B[10] = {1, 3, 5, 7, 9, 1, 1, 2, 1, 2}; 
     int r;

     r = sumArray(B, 10);
     printf("r = %d\n", r);
  }  

We are passing B which has the type reference to int

Another way to define functions with array parameters

An array parameter defined as int A[ ] in C can also be defined as int *:

  #include <stdio.h>

  int sumArray( int *A , int n /*array length*/ )
  {
     int i, s;

     s = 0;
     for (i = 0; i < n; i++)
        s += A[i];  // A[i] ≡ *(A+i) !!
     return(s);
  }

  int main(int argc, char *argv[])
  {
     int B[10] = {1, 3, 5, 7, 9, 1, 1, 2, 1, 2}; 
     int r;

     r = sumArray(B, 10);
     printf("r = %d\n", r);
  }  

So when you see a int * typed parameter, it could also be an array parameter in C!!!