passing pointer as function parameter

Discussion in 'C Programming' started by Roman Mashak, Sep 14, 2006.

  1. Roman Mashak

    Roman Mashak Guest

    Hello,

    I belive the reason of problem is simple, but can't figure out.

    This is piece of code:

    struct timeval {
    long tv_sec; /* seconds */
    long tv_usec; /* microseconds */
    };

    extern void proc_v4(char *, ssize_t, struct timeval *);
    ....

    int main(void)
    {
    static char recvbuf[BUFSIZE];
    ssize_t n;
    struct timeval tv2;

    ...

    proc_v4(recvbuf, n, &tv2);
    }

    void proc_v4(char *ptr, ssize_t len, struct timeval *tvsend)
    {
    struct icmp *icmp;
    ...
    /* here I parse IP packet and address pointer on ICMP packet */
    ...
    tvsend = (struct timeval *) icmp->icmp_data;
    }

    The problem is after calling proc_v4() the value of 'tv2' doesn't change,
    what I can't understand, because I pass pointer, and then re-assign pointer
    into another one.

    Where is the problem? Thanks a lot.

    With best regards, Roman Mashak. E-mail:
     
    Roman Mashak, Sep 14, 2006
    #1
    1. Advertising

  2. Roman Mashak

    T.M. Sommers Guest

    Roman Mashak wrote:
    > Hello,
    >
    > I belive the reason of problem is simple, but can't figure out.
    >
    > This is piece of code:
    >
    > struct timeval {
    > long tv_sec; /* seconds */
    > long tv_usec; /* microseconds */
    > };
    >
    > extern void proc_v4(char *, ssize_t, struct timeval *);
    > ...
    >
    > int main(void)
    > {
    > static char recvbuf[BUFSIZE];
    > ssize_t n;
    > struct timeval tv2;
    >
    > ...
    >
    > proc_v4(recvbuf, n, &tv2);
    > }
    >
    > void proc_v4(char *ptr, ssize_t len, struct timeval *tvsend)
    > {
    > struct icmp *icmp;
    > ...
    > /* here I parse IP packet and address pointer on ICMP packet */
    > ...
    > tvsend = (struct timeval *) icmp->icmp_data;


    What you probably want to do is assign values to the elements of
    tvsend. Even if you had done everything else right (and you
    haven't), icmp->icmp_data might cease to exist when proc_v4 ends,
    depending on where it came from, so you don't want to keep a
    pointer to it unless you know that it will live on.

    > }
    >
    > The problem is after calling proc_v4() the value of 'tv2' doesn't change,
    > what I can't understand, because I pass pointer, and then re-assign pointer
    > into another one.
    >
    > Where is the problem? Thanks a lot.


    You aren't doing what you think you are doing. What you think
    you are doing is something like:

    struct timeval *tv2;
    proc_v4(..., &tv2);

    This is very different from what you are really doing.

    --
    Thomas M. Sommers -- -- AB2SB
     
    T.M. Sommers, Sep 14, 2006
    #2
    1. Advertising

  3. Roman Mashak wrote:

    > void proc_v4(char *ptr, ssize_t len, struct timeval *tvsend)
    > {
    > struct icmp *icmp;
    > ...
    > /* here I parse IP packet and address pointer on ICMP packet */
    > ...
    > tvsend = (struct timeval *) icmp->icmp_data;

    This line assigns to the tvsend pointer. the tvsend pointer
    is local to this function.

    You want to assign to what it points at rather, as the caller pointed
    it at a struct timeval it can access:

    *tvsend = *(struct timeval *) icmp->icmp_data;

    Or
    struct timeval *tmp

    tmp = (struct timeval *) icmp->icmp_data;
    tvsend->tv_sec = tmp->tv_sec;
    tvsend->tv_usec = tmp->tv_usec;

    > }


    Another problem is icmp->icmp_data; might indeed not values
    a struct timeval that your platform can understand..
    (alignment problems, padding and endianess might skew things)
     
    =?ISO-8859-1?Q?=22Nils_O=2E_Sel=E5sdal=22?=, Sep 14, 2006
    #3
  4. Roman Mashak

    Default User Guest

    Roman Mashak wrote:


    > void proc_v4(char *ptr, ssize_t len, struct timeval *tvsend)
    > {
    > struct icmp *icmp;
    > ...
    > /* here I parse IP packet and address pointer on ICMP packet */
    > ...
    > tvsend = (struct timeval *) icmp->icmp_data;
    > }
    >
    > The problem is after calling proc_v4() the value of 'tv2' doesn't
    > change, what I can't understand, because I pass pointer, and then
    > re-assign pointer into another one.


    That's covered in the FAQs:

    <http://c-faq.com/ptrs/passptrinit.html>




    Brian
     
    Default User, Sep 14, 2006
    #4
  5. Roman Mashak

    Roman Mashak Guest

    Hello, Default!
    You wrote on 14 Sep 2006 17:59:36 GMT:

    ??>> void proc_v4(char *ptr, ssize_t len, struct timeval *tvsend)
    ??>> {
    ??>> struct icmp *icmp;
    ??>> ...
    ??>> /* here I parse IP packet and address pointer on ICMP packet */
    ??>> ...
    ??>> tvsend = (struct timeval *) icmp->icmp_data;
    ??>> }
    ??>>
    ??>> The problem is after calling proc_v4() the value of 'tv2' doesn't
    ??>> change, what I can't understand, because I pass pointer, and then
    ??>> re-assign pointer into another one.

    DU> That's covered in the FAQs:

    DU> <http://c-faq.com/ptrs/passptrinit.html>
    Hm, I'm confused. FAQ reecommends to pass the function address of pointer.
    Is it considered to be a "right standard confirming way" ?
    On the other hand: passing just pointer and assigning the value to what the
    pointer points at (as was proposed by Nils O. Selasdal) - works well.

    Which way is correct?

    Thank you.

    With best regards, Roman Mashak. E-mail:
     
    Roman Mashak, Sep 15, 2006
    #5
  6. "Roman Mashak" <> writes:
    > Hello, Default!
    > You wrote on 14 Sep 2006 17:59:36 GMT:
    >
    > ??>> void proc_v4(char *ptr, ssize_t len, struct timeval *tvsend)
    > ??>> {
    > ??>> struct icmp *icmp;
    > ??>> ...
    > ??>> /* here I parse IP packet and address pointer on ICMP packet */
    > ??>> ...
    > ??>> tvsend = (struct timeval *) icmp->icmp_data;
    > ??>> }
    > ??>>
    > ??>> The problem is after calling proc_v4() the value of 'tv2' doesn't
    > ??>> change, what I can't understand, because I pass pointer, and then
    > ??>> re-assign pointer into another one.
    >
    > DU> That's covered in the FAQs:
    >
    > DU> <http://c-faq.com/ptrs/passptrinit.html>
    > Hm, I'm confused. FAQ reecommends to pass the function address of pointer.
    > Is it considered to be a "right standard confirming way" ?
    > On the other hand: passing just pointer and assigning the value to what the
    > pointer points at (as was proposed by Nils O. Selasdal) - works well.
    >
    > Which way is correct?


    It depends on what you want to do.

    In general, if you have a FOO, and you want a function to modify its
    value for you, you need to pass the function a FOO* (i.e., you pass it
    a pointer to a FOO so it can modify your FOO).

    If the FOO itself happens to be a pointer to something (i.e., you want
    the function to modify a pointer object for you), then passing a FOO*
    means passing a pointer-to-pointer-to-something.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 15, 2006
    #6
  7. Roman Mashak

    Default User Guest

    Roman Mashak wrote:

    > Hello, Default!
    > You wrote on 14 Sep 2006 17:59:36 GMT:


    > DU> <http://c-faq.com/ptrs/passptrinit.html>
    > Hm, I'm confused. FAQ reecommends to pass the function address of
    > pointer. Is it considered to be a "right standard confirming way" ?
    > On the other hand: passing just pointer and assigning the value to
    > what the pointer points at (as was proposed by Nils O. Selasdal) -
    > works well.
    >
    > Which way is correct?


    Ok, I think I confused your post with one in a different thread.




    Brian
     
    Default User, Sep 15, 2006
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Newsgroup - Ann
    Replies:
    5
    Views:
    611
    John Carson
    Jul 30, 2003
  2. James Vanns
    Replies:
    7
    Views:
    7,049
    Evan Carew
    Jan 21, 2004
  3. jimjim
    Replies:
    16
    Views:
    844
    Jordan Abel
    Mar 28, 2006
  4. Vijai Kalyan
    Replies:
    4
    Views:
    713
    Vijai Kalyan
    Nov 8, 2005
  5. Mister B
    Replies:
    8
    Views:
    554
    Nick Keighley
    Aug 26, 2010
Loading...

Share This Page