Thread: Little more help for ME

  1. #1

    Exclamation Little more help for ME

    Yes I have one more question for the great people at the C board.

    What I am wonder ing is in the code below hoe does the function change() multiply *pnumber by 2 if *pnumber is not given a value in that function and is not globally declared?

    #include <stdio.h>

    int change(int* pnumber);

    void main()
    int number = 10;
    int* pnumber = &number;
    int result = 0;

    result = change(pnumber);
    printf("\n In main, result = %d\t number = %d", result, number);

    int change(int* pnumber)
    *pnumber *= 2;
    printf("\n In function change, *pnumber = %d", *pnumber);
    return *pnumber;
    Give me a bad reputation!!!

  2. #2
    Registered User
    Join Date
    Oct 2001
    pnumber is passed to the function change()

    result = change(pnumber);

    therefore the value that pnumber points to can be multiplied by 2 in change() because change() has a copy of the pointer.

    Hope that makes sense,

    (: Ian

  3. #3
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Because you've passed the address into the function. A copy of this address is used within the function, but as the pointer has been de-referenced you're multiplying the value stored at this address.

Popular pages Recent additions subscribe to a feed