Web Design and Web Development Forum

  1. #1
    Join Date
    Jul 2006
    Posts
    19
    Rep Power
    0
  2. ravi.xolve is on a distinguished road
  3. Question double pointer to structure

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    struct a
    {
            int a;
            char c;
    };
    main()
    {
            struct a *p, q;
            void func(struct a **);
            printf("Previous address p: %u",p);
            func(&p);
            printf("\nNew address p: %u",p);
            printf("\nValues stored in p: %d %c\n",p->a,p->c);
    }
    
    void func(struct a **ptr)
    {
            *ptr=(struct a *)malloc(sizeof(struct a));
            printf("\n*********%u",*ptr);
            *ptr->a=100;
            *ptr->c='f';
            /*try reducing and addind the *s */
    }
    why the above code gives the error and i cant help it while the code is correct logically. pls help soooooooon!
    Last edited by hot_cakes; 11-18-2006 at 10:56 AM. Reason: added code tags for clarity
    Reply With Quote Reply With Quote
  4. #2
    gorda001's Avatar
    Join Date
    Jun 2005
    Location
    <0x79a3f6>
    Posts
    4,851
    Rep Power
    11
  5. gorda001 is on a distinguished road
  6. Re: double pointer to structure

    Just a quick note before HC posts his large explanation...

    It really is a bad idea to write code like:
    Code:
    struct a *p, q;
    Because it's really unclear. Instead write:
    Code:
    struct a *p;
    struct a q;
    Reply With Quote Reply With Quote
  7. #3
    Join Date
    Mar 2006
    Location
    Glasgow
    Age
    28
    Posts
    687
    Rep Power
    7
  8. Mythic is on a distinguished road
  9. Re: double pointer to structure

    Quote gorda001 originally posted: View Post
    Just a quick note before HC posts his large explanation...
    Yeah! I'm gonna stop replying to c or c++ related posts, his answers always go into so much detail that mine just look stupid :-P
    Wisdom Begins In Wonder ~ Socrates
    Reply With Quote Reply With Quote
  10. #4
    gorda001's Avatar
    Join Date
    Jun 2005
    Location
    <0x79a3f6>
    Posts
    4,851
    Rep Power
    11
  11. gorda001 is on a distinguished road
  12. Re: double pointer to structure

    It's funny that we are having this discussion still while he is writing :)
    Reply With Quote Reply With Quote
  13. #5
    hot_cakes's Avatar
    Join Date
    Aug 2005
    Location
    Bristol, UK
    Age
    30
    Posts
    2,913
    Rep Power
    9
  14. hot_cakes will become famous soon enough
  15. Re: double pointer to structure

    Within func(), your printf statement has a conversion string that specifies an unsigned int is going to be given, but the argument you give is *ptr which is of type struct a* and not unsigned int.

    You make similar printf calls elsewhere. To print the address represented by a pointer use %p, not %u. You'll need to cast your second arguments to printf to void*, too.

    Where you have for example, *ptr->a=100, you should instead have (*ptr)->a=100 because -> takes precedence over * when used as a dereferencing operator.

    You shouldn't cast the return value of malloc. Some older books will tell you to do so, but it's best avoided because it might hide errors. malloc (and calloc and realloc) return void* so there's no need to a cast.

    Don't forget to free what you have malloc-ed, though you probably know that.

    'a' probably isn't a very good name for a struct, especially if that struct has a member named 'a'!

    A more idiomatic way of doing what you're doing is:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    struct my_type
    {
        int x;
    };
    
    struct my_type *create_my_type(int value)
    {
        struct my_type *ret = malloc(sizeof *ret);
        if (ret) /* don't forget that malloc can fail!! */
            ret->x = value;
    
        return ret;
    }
    
    int main(void)
    {
        struct my_type *p = create_my_type(5);
        if (p)
            printf("p is %p, p->x is %d\n", (void *)p, p->x);
        else
            printf("Failed to create a my_type");
    
        free(p);
    
        return 0; 
    }
    Edd
    Last edited by hot_cakes; 11-18-2006 at 11:16 AM. Reason: typos
    Visit me at: mr-edd.co.uk
    Languages: Python | Lua
    Compilers: MinGW | MSVC++9
    Libraries: Boost | gtkmm
    Reference: Dinkumware | a.c.l.l.c-c++ FAQ
    Reply With Quote Reply With Quote
  16. #6
    hot_cakes's Avatar
    Join Date
    Aug 2005
    Location
    Bristol, UK
    Age
    30
    Posts
    2,913
    Rep Power
    9
  17. hot_cakes will become famous soon enough
  18. Re: double pointer to structure

    Quote gorda001 originally posted: View Post
    It's funny that we are having this discussion still while he is writing :)
    Grrr ;)

    Edd
    Visit me at: mr-edd.co.uk
    Languages: Python | Lua
    Compilers: MinGW | MSVC++9
    Libraries: Boost | gtkmm
    Reference: Dinkumware | a.c.l.l.c-c++ FAQ
    Reply With Quote Reply With Quote
  19. #7
    Join Date
    Mar 2006
    Location
    Glasgow
    Age
    28
    Posts
    687
    Rep Power
    7
  20. Mythic is on a distinguished road
  21. Re: double pointer to structure

    Quote hot_cakes originally posted: View Post
    Grrr ;)

    Edd

    I bet IRL you're just like JP from that film Grandma's Boy!! :)

    That's not an insult, incase you've not seen the film!
    Wisdom Begins In Wonder ~ Socrates
    Reply With Quote Reply With Quote

Similar Threads

  1. Forum Coding
    By DominantSpecies in forum HTML/CSS Coding
    Replies: 2
    Last Post: 04-24-2006, 01:53 PM
  2. QUIZ! : 10 common pointer and memory allocation pitfalls in C and C++
    By hot_cakes in forum Articles, Tutorials, and Guides
    Replies: 2
    Last Post: 08-31-2005, 12:15 PM
  3. Source and Driver Program
    By felice05 in forum Java Programming
    Replies: 2
    Last Post: 08-05-2005, 04:13 PM