Pass by Ref vs. Value

Standard Input/Output
  • Pass by references vs. by values Source Code

    /*

     * File:   main.c

     * Author: phamvanvung

     *

     * Created on September 21, 2014, 8:16 PM

     */

     

    #include <stdio.h>

    #include <stdlib.h>

     

    /*

     *

     */

    int main(int argc, char** argv) {

        int x;

        x = 15;

        printf("memory location of x %d\n", &x);

        int* ptr1 = &x;

        int y;

        y = x;

        printf("y is %d\n", y);

        printf("Changing y into 18\n");

        y = 18;

        printf("y is %d\n", y);

        printf("x is %d\n", x);

       

        int* ptr2;

        ptr2 = &x;//ptr2=ptr1;

        //Changing value of x using its pointer to 19

        *ptr2 = 19;

        printf("Value of x is %d\n", x);

        printf("Value of x using pointer1 to access is %d\n", *ptr1);

        return (EXIT_SUCCESS);

    }