Re: arrow operator

From: Dan Pop (Dan.Pop_at_cern.ch)
Date: 10/29/03


Date: 29 Oct 2003 16:05:47 GMT

In <6d4be9e6.0310290724.61daf35d@posting.google.com> jasclz@aol.com (Jason) writes:

>I was trying to pass a pointer to a structure to a function, where the
>pointer is pointing to one of the members in the structure, is this
>possible???
>
>For Example:
>
>struct reptile *animalPtr
>
>foo(animalPtr->snakes);
>foo(animalPtr->aligator);

You're not passing any pointer to foo, you're passing the value of a
struct reptile member.

>/* here now animalPtr->snakes == 100 and
> animalPtr->aligaotr == 100 */
>
>Within foo, I was hoping to be able to change the value of the object
>in the structure.

For this purpose, foo really needs a pointer to the structure.

>For Example:
>
>foo(???){ /* not sure what the parameters will be */
>
> if(blah < blah2)
> ??? = 100 /* where ??? is the input to this function */
>
>}
>
>I was wondering would this work? If so, what would my ??? in my
>example be? How would I be able to change the value of ??? ? If this
>is not possible, what how should I go about doing this? Thanks in
>advance for all replies.

To make this to work, you must make up your mind about what member of the
struct reptile you want to set in the function. The ??? in the function
definition CANNOT be the same thing as the ??? in the assignment, if you
want to change anything in the struct whose address is passed to foo.

    void foo(struct reptile *p)
    {
        if(blah < blah2) p -> snakes = 100;
    }

This assumes that snakes is a member of structure reptile. And the
proper way of calling this function is:

    struct reptile {
        int snakes;
        int alligator;
        ...
    } animal = { ... };

    foo(&animal);

You can't use pointers to structures without having the structures to
which they point in the first place.

OTOH, if what you want foo() to do is to change the value of an arbitrary
member in the struct, but whose type is known at compile time, you don't
need pointers to structs at all, a pointer to int is all that is needed
in our example:

    void foo2(int *p)
    {
        if(blah < blah2) *p = 100;
    }

and you have to pass it the address of the member whose value you want
to change:

    foo2(&animal.snakes);

It is not clear from your example what exactly you want. Keep in mind
that -> dereferences a pointer to struct and also selects a struct
member, so the following two assignments are having exactly the same
effect:

    struct reptile *p = &animal;
    animal.snakes = 100;
    p -> snakes = 100;

Dan

-- 
Dan Pop
DESY Zeuthen, RZ group
Email: Dan.Pop@ifh.de


Relevant Pages

  • Re: C++ in the kernel
    ... private struct foo { ... for instance to cache a function pointer for lazy binding ... list_insert_before(elem, elem {,head ?}) ...
    (freebsd-arch)
  • Re: how to store list of varying types
    ... represent that with those in the struct definitions? ... pointer null, and the second one the CString object. ... then have to finish constructing the packet by copying the two data objects ... start with an upper-case letter (so, I would use DataElement ...
    (microsoft.public.vc.mfc)
  • Re: Using offsetof to create a pointer to the start of a struct?
    ... dereference a pointer to a struct element to create a pointer to the start ... member that is not the first member in the struct. ... subtracting the hand calculated offset of the member, ...
    (comp.lang.c)
  • Re: N1298 - try/finally for C
    ... Do you mean that "struct throwInfo" should be optional? ... that exception handling would be supported only on implementations ... If the standard mandates an integer as throw argument, ... should be an integer wide enough to encode a pointer if necessary. ...
    (comp.std.c)
  • Re: Memory Structure Pointer Problems
    ... typedef struct sta { ... char* name; ... int num_cmpnds; ... A pointer to a struct cmp is almost ...
    (comp.lang.c)