The parameter type dataType a[ ] and the parameter type dataType *a in C

  • The parameter int a[ ] in a C function represents an array as parameter:

     void f(int a[ ]) // a is the base address of an array of int variables
     {
        ...
     }
    
     The parameter a contains the base address of an int typed array:
    
                      |       |
    		  +-------+
    	     a:	  |  7000 |
    		  +-------+
    		  |  ...  |
    		  |  ...  |
    		  +-------+
    	    7000  |  int  | <--- there is an int type array    here
    		  +-------+
    		  |  int  |
    		  +-------+
    		  |  int  |
    		  +-------+
    		  |  ...  |
    
    

     

     

The parameter type dataType a[ ] and the parameter type dataType *a in C

  • The parameter int *a in a C function represents an reference variable as parameter:

     void f(int *a)   // a is the      address of int typed variable
     {
        ...
     }
    
     The parameter a contains the      address of an int typed variable:
    
                      |       |
    		  +-------+
    	     a:	  |  7000 |
    		  +-------+
    		  |  ...  |
    		  |  ...  |
    		  +-------+
    	    7000  |  int  | <--- there is an int type variable here
    		  +-------+
    		  |  ???  |
    		  +-------+
    		  |  ???  |
    		  +-------+
    		  |  ...  |
    
    

    Notice:   there is no difference in the interpretation of int a[ ] and int *a !!!

    In both cases, the parameter a contains the address of an int typed variable !!!

In C: the declaration a[] and the declaration *a are equivalent

  • We have seen that the notation double list[] declares an array of double as parameter:

        void selectionSort(double list[], int N) 
        {
            for (int k = 0; k < N-1; k++)
            {
                // Find the minimum value and its index in list[k..N-1]
                double min = list[k];    // Assume first element is min
                int    indexOfMin = k;   // Index of current min
    
                for ( int i = k+1; i < N; i++ )
                    if ( list[i] < min ) // We found a smaller element
                    {
                        min = list[i];   // Update min value
                        indexOfMin = i;  // Update its index in array
                    }
       
                // Swap list[k] with list[indexOfMin] if necessary
                if ( indexOfMin != k )
                {   // Swap list[indexOfMin] and list[k]
                    double help = list[indexOfMin];
                    list[indexOfMin] = list[k];
                    list[k] = help;
                }
            } 
        }

    Notice:   the expression list[k] is using array member selection

In C: the declaration a[] and the declaration *a are equivalent

  • We can also use double *list to declare an array of double as parameter:

        void selectionSort(double *list, int N) 
        {
            for (int k = 0; k < N-1; k++)
            {
                // Find the minimum value and its index in list[k..N-1]
                double min = list[k];    // Assume first element is min
                int    indexOfMin = k;   // Index of current min
    
                for ( int i = k+1; i < N; i++ )
                    if ( list[i] < min ) // We found a smaller element
                    {
                        min = list[i];   // Update min value
                        indexOfMin = i;  // Update its index in array
                    }
       
                // Swap list[k] with list[indexOfMin] if necessary
                if ( indexOfMin != k )
                {   // Swap list[indexOfMin] and list[k]
                    double help = list[indexOfMin];
                    list[indexOfMin] = list[k];
                    list[k] = help;
                }
            } 
        }

    Notice:   the expression list[k] is using short hand [ ] with pointer arithmetic

DEMO: demo/C/set1/selectionSort2.c