Little more help for ME

This is a discussion on Little more help for ME within the C Programming forums, part of the General Programming Boards category; Yes I have one more question for the great people at the C board. What I am wonder ing is ...

  1. #1
    Perverted unanimous's Avatar
    Join Date
    Oct 2001
    Posts
    336

    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
    Ian
    Ian is offline
    Registered User
    Join Date
    Oct 2001
    Posts
    21
    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
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    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.
    zen

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21