Passing parameters in the C Programming Language

  1. The by-reference passing mechanism is many languages have implied operations that the computer (will) perform to obtain the actual parameter.

  2. The C programming language is interesting because it takes a "what you see is what you get" approach.

  3. The relevant operators in the C programming language:

    Operators in C that are relevant in parameter passing:
          &(x)    returns the the address of variable x
          *(y)    tells the computer to go to the address stored in
    	      variable y to get a variable


    DEMO program: click here

  4. You can see accesing by-reference parameters explicitly in C:

    Passing parameter by reference in C:
          #include <stdio.h>
          void myMethod(int* a) // i contains a reference to an int variable
             *(a) = *(a) + 1;   // Use variable at the reference location
          int main(int argc, char **argv)    
    	 int i = 10;
    	 myMethod(&i);            // Pass address of i
    	 printf("i = %d\n", i);   // Prints i

    DEMO program: click here

  5. NOTE: pass-by-value is no different from what you have seen: click here