Pointer problem

Discussion in 'C Programming' started by seema, Dec 9, 2005.

  1. seema

    seema Guest

    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,"playing one"},
    {0,0x00000002L,COLLATE,"playing two"},
    {0,0x00000004L,COLLATE,"playing three"},
    {0,0x00000008L,COLLATE,"playing four"},
    {0,0x00000008L,COLLATE,"playing five"},
    {0,0x00000008L,COLLATE,"playing six"},
    };
    void *dinku;
    dinku=(struct mytips*)malloc((sizeof(char)*sizeof(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
     
    seema, Dec 9, 2005
    #1
    1. Advertising

  2. seema

    Michael Mair Guest

    seema wrote:
    > I have a struct and I want to access its values, I am new to pointers
    > so I have written something like this,


    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>

    > # define COLLATE "Beetle"
    > struct mytips
    > {
    > short inverse;
    > long mass;
    > char *c;
    > char *m;
    > };
    > int main(){

    make that
    int main (void)
    > struct mytips store[10] = {
    > {0,0x00000001L,COLLATE,"playing one"},
    > {0,0x00000002L,COLLATE,"playing two"},
    > {0,0x00000004L,COLLATE,"playing three"},
    > {0,0x00000008L,COLLATE,"playing four"},
    > {0,0x00000008L,COLLATE,"playing five"},

    You probably wanted 0x00000010L
    > {0,0x00000008L,COLLATE,"playing six"},
    > };
    > void *dinku;
    > dinku=(struct mytips*)malloc((sizeof(char)*sizeof(store))+1);


    Wrong idiom.
    Usually, you do
    struct mytips *dinku;
    dinku = malloc(NumberOfMytips * 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;
    > }
    > Now how can I access the values of store using void pointer? Can
    > somebody explaing
    > how to do it


    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.
     
    Michael Mair, Dec 9, 2005
    #2
    1. Advertising

  3. On Thu, 08 Dec 2005 23:25:38 -0800, seema wrote:

    > 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,"playing one"},
    > {0,0x00000002L,COLLATE,"playing two"},
    > {0,0x00000004L,COLLATE,"playing three"},
    > {0,0x00000008L,COLLATE,"playing four"},
    > {0,0x00000008L,COLLATE,"playing five"},
    > {0,0x00000008L,COLLATE,"playing six"},
    > };
    > void *dinku;
    > dinku=(struct mytips*)malloc((sizeof(char)*sizeof(store))+1);


    Casting after malloc is unneccesary. Include <stdlib.h> instead.

    > Now how can I access the values of store using void pointer?


    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.
     
    Kleuskes & Moos, Dec 9, 2005
    #3
  4. Kleuskes & Moos said:

    > On Thu, 08 Dec 2005 23:25:38 -0800, seema wrote:
    >
    >> 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,"playing one"},
    >> {0,0x00000002L,COLLATE,"playing two"},
    >> {0,0x00000004L,COLLATE,"playing three"},
    >> {0,0x00000008L,COLLATE,"playing four"},
    >> {0,0x00000008L,COLLATE,"playing five"},
    >> {0,0x00000008L,COLLATE,"playing six"},
    >> };
    >> void *dinku;
    >> dinku=(struct mytips*)malloc((sizeof(char)*sizeof(store))+1);

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

    >
    > 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.


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

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Dec 9, 2005
    #4
  5. On Fri, 09 Dec 2005 17:04:13 +0000, Richard Heathfield wrote:

    <snip>
    >>> Now how can I access the values of store using void pointer?

    >>
    >> 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.

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


    Me and my big mouth. :).

    This is better, of course. Use 'void *' only if you have no choice.
     
    Kleuskes & Moos, Dec 9, 2005
    #5
  6. Kleuskes & Moos said:

    > On Fri, 09 Dec 2005 17:04:13 +0000, Richard Heathfield wrote:
    >
    >> {
    >> struct mytips *p = dinku; /* cast? what cast? */
    >> }

    >
    > Me and my big mouth. :).
    >
    > This is better, of course. Use 'void *' only if you have no choice.


    ....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
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Dec 9, 2005
    #6
    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. Replies:
    10
    Views:
    717
    Chris Torek
    Feb 4, 2005
  2. jimjim
    Replies:
    16
    Views:
    852
    Jordan Abel
    Mar 28, 2006
  3. Replies:
    4
    Views:
    1,286
    Fred Zwarts
    Jul 2, 2009
  4. A
    Replies:
    7
    Views:
    644
  5. , India

    pointer to an array vs pointer to pointer

    , India, Sep 20, 2011, in forum: C Programming
    Replies:
    5
    Views:
    464
    James Kuyper
    Sep 23, 2011
Loading...

Share This Page