Pointer problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • seema

    #1

    Pointer problem

    I have a struct and I want to access its values, I am new to pointers
    so I have written something like this,
    # define COLLATE "Beetle"
    struct mytips
    {
    short inverse;
    long mass;
    char *c;
    char *m;
    };
    int main(){
    struct mytips store[10] = {
    {0,0x00000001L, COLLATE,"playin g one"},
    {0,0x00000002L, COLLATE,"playin g two"},
    {0,0x00000004L, COLLATE,"playin g three"},
    {0,0x00000008L, COLLATE,"playin g four"},
    {0,0x00000008L, COLLATE,"playin g five"},
    {0,0x00000008L, COLLATE,"playin g six"},
    };
    void *dinku;
    dinku=(struct mytips*)malloc( (sizeof(char)*s izeof(store))+1 );
    }
    Now how can I access the values of store using void pointer? Can
    somebody explaing
    how to do it
    Thanks in advance,
    Seema

  • Michael Mair

    #2
    Re: Pointer problem

    seema wrote:[color=blue]
    > I have a struct and I want to access its values, I am new to pointers
    > so I have written something like this,[/color]

    No. Nononononono!
    Please copy and paste the actual code.
    "something like this" may differ in important points
    from the posted code. This makes it unnecessarily hard
    to help you.
    BTW: Creating a minimal example helps people see your
    problem at a glance...

    Your code lacks

    #include <stdlib.h>
    [color=blue]
    > # define COLLATE "Beetle"
    > struct mytips
    > {
    > short inverse;
    > long mass;
    > char *c;
    > char *m;
    > };
    > int main(){[/color]
    make that
    int main (void)[color=blue]
    > struct mytips store[10] = {
    > {0,0x00000001L, COLLATE,"playin g one"},
    > {0,0x00000002L, COLLATE,"playin g two"},
    > {0,0x00000004L, COLLATE,"playin g three"},
    > {0,0x00000008L, COLLATE,"playin g four"},
    > {0,0x00000008L, COLLATE,"playin g five"},[/color]
    You probably wanted 0x00000010L[color=blue]
    > {0,0x00000008L, COLLATE,"playin g six"},
    > };
    > void *dinku;
    > dinku=(struct mytips*)malloc( (sizeof(char)*s izeof(store))+1 );[/color]

    Wrong idiom.
    Usually, you do
    struct mytips *dinku;
    dinku = malloc(NumberOf Mytips * sizeof *dinku);

    Apart from that: sizeof(char) == 1, always.
    I do not understand in the least what you are trying to achieve
    with the above.
    You forgot:
    if (dinku == NULL) {
    /* error handling */
    }
    .... /* use dinku */
    free(dinku);

    and finally
    return 0;[color=blue]
    > }
    > Now how can I access the values of store using void pointer? Can
    > somebody explaing
    > how to do it[/color]

    No. Yes.
    If you passed a pointer to an object as a void *, access
    it as the right type:
    int ireceivedinkus( void *pData)
    {
    struct mytips *pDinku = pData;
    /* access data via pDinku */
    ....
    }

    You can convert between void* and other pointer types without
    explicit cast.
    Best, you just read the comp.lang.c FAQ (google will point you
    there). There is enough background about pointers.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.

    Comment

    • Kleuskes & Moos

      #3
      Re: Pointer problem

      On Thu, 08 Dec 2005 23:25:38 -0800, seema wrote:
      [color=blue]
      > I have a struct and I want to access its values, I am new to pointers
      > so I have written something like this,
      > # define COLLATE "Beetle"
      > struct mytips
      > {
      > short inverse;
      > long mass;
      > char *c;
      > char *m;
      > };
      > int main(){
      > struct mytips store[10] = {
      > {0,0x00000001L, COLLATE,"playin g one"},
      > {0,0x00000002L, COLLATE,"playin g two"},
      > {0,0x00000004L, COLLATE,"playin g three"},
      > {0,0x00000008L, COLLATE,"playin g four"},
      > {0,0x00000008L, COLLATE,"playin g five"},
      > {0,0x00000008L, COLLATE,"playin g six"},
      > };
      > void *dinku;
      > dinku=(struct mytips*)malloc( (sizeof(char)*s izeof(store))+1 );[/color]

      Casting after malloc is unneccesary. Include <stdlib.h> instead.
      [color=blue]
      > Now how can I access the values of store using void pointer?[/color]

      You must cast the void pointer, otherwise there's no way to access any of
      the fields, since the compiler does not know what is actually referenced.

      Comment

      • Richard Heathfield

        #4
        Re: Pointer problem

        Kleuskes & Moos said:
        [color=blue]
        > On Thu, 08 Dec 2005 23:25:38 -0800, seema wrote:
        >[color=green]
        >> I have a struct and I want to access its values, I am new to pointers
        >> so I have written something like this,
        >> # define COLLATE "Beetle"
        >> struct mytips
        >> {
        >> short inverse;
        >> long mass;
        >> char *c;
        >> char *m;
        >> };
        >> int main(){
        >> struct mytips store[10] = {
        >> {0,0x00000001L, COLLATE,"playin g one"},
        >> {0,0x00000002L, COLLATE,"playin g two"},
        >> {0,0x00000004L, COLLATE,"playin g three"},
        >> {0,0x00000008L, COLLATE,"playin g four"},
        >> {0,0x00000008L, COLLATE,"playin g five"},
        >> {0,0x00000008L, COLLATE,"playin g six"},
        >> };
        >> void *dinku;
        >> dinku=(struct mytips*)malloc( (sizeof(char)*s izeof(store))+1 );[/color]
        >
        > Casting after malloc is unneccesary. Include <stdlib.h> instead.
        >[color=green]
        >> Now how can I access the values of store using void pointer?[/color]
        >
        > You must cast the void pointer, otherwise there's no way to access any of
        > the fields, since the compiler does not know what is actually referenced.[/color]

        {
        struct mytips *p = dinku; /* cast? what cast? */
        }

        --
        Richard Heathfield
        "Usenet is a strange place" - dmr 29/7/1999

        email: rjh at above domain (but drop the www, obviously)

        Comment

        • Kleuskes & Moos

          #5
          Re: Pointer problem

          On Fri, 09 Dec 2005 17:04:13 +0000, Richard Heathfield wrote:

          <snip>[color=blue][color=green][color=darkred]
          >>> Now how can I access the values of store using void pointer?[/color]
          >>
          >> You must cast the void pointer, otherwise there's no way to access any of
          >> the fields, since the compiler does not know what is actually referenced.[/color]
          >
          > {
          > struct mytips *p = dinku; /* cast? what cast? */
          > }[/color]

          Me and my big mouth. :-).

          This is better, of course. Use 'void *' only if you have no choice.

          Comment

          • Richard Heathfield

            #6
            Re: Pointer problem

            Kleuskes & Moos said:
            [color=blue]
            > On Fri, 09 Dec 2005 17:04:13 +0000, Richard Heathfield wrote:
            >[color=green]
            >> {
            >> struct mytips *p = dinku; /* cast? what cast? */
            >> }[/color]
            >
            > Me and my big mouth. :-).
            >
            > This is better, of course. Use 'void *' only if you have no choice.[/color]

            ....or rather, use it when it's the Right Thing. Sometimes void * is not the
            *only* choice, but is nevertheless the most natural choice that leads to
            the most elegant code. The only real problem with it is that, by its very
            nature, it sidesteps type-checking. That, of course, is also its great
            asset.

            --
            Richard Heathfield
            "Usenet is a strange place" - dmr 29/7/1999

            email: rjh at above domain (but drop the www, obviously)

            Comment

            Working...