Ramoth's bumped pointer question (was How did you...)

This is a discussion on Ramoth's bumped pointer question (was How did you...) within the C Programming forums, part of the General Programming Boards category; I (newbie) have a question on pointers. I'll paste the code. I know how the main sends to the call, ...

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    I (newbie) have a question on pointers. I'll paste the code. I know how the main sends to the call, but after that I'm lost. Can anyone explain what happens? The program runs and the execution is: "In hi and d = 16"; "In hi again and d = 23"; "In Main and a=8, b=15, c=5". So I know the answers but no clue how it works

    Code:
    #include <stdio.h>
    void ho(int *k, int l, int *m);
    void hi(int *x, int *y, int d);
    
    int main(void)
    {   int a,b,c;
         c=5;
         hi(&a, &b, c);
         printf("In main and a=%d, b=%d, c=%d",a,b,c);
         return (0);
    }
    
    void ho(int *k, int l, int *m)
    {    *k = 5 * l;
          *m = l + 20;
    }
    
    void hi(int *x, int *y, int d)
    {    int a;
          a=3;
         *x = 8;
         d = *x + d + a;
         printf("In hi and d = %d\n", d);
         ho(y, a, &d);
         printf("In hi again and d = %d\n", d);
    }

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    hi(&a, &b, c);
    This means that the address of a and b are sent, while the value of c is sent.
    The former is called "passing by reference."

    *x = 8;
    Because you sent the address of x, you cannot reference it directly. You want the value of x to be 8, not the address, so you call * x.

    Need more help?

    Also, you should have started a new thread rather then bumped this one

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    Thanks MadCow. I think if this ever gets cleared up for me, I'll have a much better understanding of pointers.

    I get the first send, and why the answer is 16.

    But to clarify how it works, I'm trying to trace the program ...
    The main passes address of a, b and value 5 to hi.

    The "hi" function sends y, a, &d; or 8, 3, address of d, which now is 16, to "ho"

    In ho, *k is 8, int l is 3, *m is 16
    Since the address of d was sent to pointer to m, ho returns (*m=3+20 =23) to d in the 2nd printf in hi .

    But back to Main - no lo comprende. Been trying to trace it in words but I'm confusing myself.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    What values don't you understand in main? a, b, c or all of them?

    c is easy. It's set to 5, then it's passed by value, so it never changes. a we can see is set to 8 in hi()
    Code:
    *x = 8; // Remember x points to a
    then it never changes again. B, gets passed through to the function ho().

    Remember, in main it's b, which is then pointed to by y in hi() which is then assigned to the pointer k in ho().

    k = y ---> &b

    But, the only time it's assigned anything is in ho().
    Code:
    *k = 5 * l;
    At the time, you know l points to the a in hi, which equals 3, so dereferencing that will give you 5 * 3. Which equals 15.
    Last edited by SlyMaelstrom; 02-28-2006 at 09:30 AM.
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    Thanks a lot - that helps a great deal! Where I especially was confused is:
    Code:
    void hi(int *x, int *y, int d)
    
    { ho(y, a, &d);
    Using y without the * passes value back to the *y pointer. I didn't get that.

    Is this correct? Since the address of d was sent to pointer to m, ho returns (*m=3+20 =23) to d in the 2nd printf in hi .

    Thanks Again!

  6. #6
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,077
    Quote Originally Posted by Ramoth
    Is this correct? Since the address of d was sent to pointer to m, ho returns (*m=3+20 =23) to d in the 2nd printf in hi .
    Yes, and no. Yes, because I know in your mind, you have the right idea. No, because you're use of certain words kind of fudges up that statement.

    For future reference, ho() doesn't return anything. It's a void function. Arguments, whether passed by value or reference are not returned. A return is when the function resolves. Arguments passed to a pointer are resolved at assignment.
    Sent from my iPadŽ

  7. #7
    Registered User
    Join Date
    Feb 2006
    Posts
    4
    Thanks SlyMaelstrom - you're a big help

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Arguments passed to a pointer are resolved at assignment.
    Presumably you meant

    "Arguments passed to a function are resolved at assignment."



    I imagine this site's tutorials have something on pointers.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Easy pointer question
    By Edo in forum C++ Programming
    Replies: 3
    Last Post: 01-19-2009, 10:54 AM
  3. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. general pointer & malloc question
    By jstn in forum C Programming
    Replies: 2
    Last Post: 05-14-2002, 10:51 AM

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