pointer to pointer how do i solve following problem?

This is a discussion on pointer to pointer how do i solve following problem? within the C Programming forums, part of the General Programming Boards category; hi everybody in a program i have the following code typedef struct game { linked_list *list; //datastruktur som jag definierat ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    19

    pointer to pointer how do i solve following problem?

    hi everybody
    in a program i have the following code

    typedef struct game {
    linked_list *list; //datastruktur som jag definierat själv
    } game;

    in the main program i have following
    game *g1 =malloc(...)
    linked_list *link=malloc(...)

    if i want to change the content of *g1 from a function i have to do it from a function like this

    void addInList (game **g, linked_list ** li) {
    *g->list=*li;
    }
    and when i call the function i do it with following code
    addInList(&g, &link)
    can somebody tell me why this piece of code doesn't work and how should the corect syntax be

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    You actually do not need to pass-by-pointer.

    Code:
    void addInList(game * g, linked_list * li)
    {
            g->list = li;
    }
    But the way that you did it should not make it not work. Only thing I can um, maybe see, is that you say g instead of g1? Maybe the problem is elsewhere.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Use [code][/code] tags.

    What do you mean by "it doesn't work"? It doesn't compile? It doesn't run?

    Tonto is right; there's nothing wrong with your code *, although you don't need pointers to pointers. Post some more of your code.

    [edit] * Other than a precendence issue. http://www.cppreference.com/operator_precedence.html [/edit]
    Last edited by dwks; 07-19-2006 at 04:35 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,162
    Code:
    void addInList (game **g, linked_list ** li) { 
    *g->list=*li; 
    }
    I believe the 2nd line actually needs to be: (*g)->list=*li;. Precedence and all...

    I know that to dereference a pointer to a struct to get to a member (without using ->) you need to do (*ptr).member and I'm assuming that -> has the same precedence as .
    If you understand what you're doing, you're not learning anything.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You're right, it does. As written, it's the same as
    Code:
    *(g->list)=*li;
    which probably wasn't what was intended.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    OMG!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf output makes no sense. Is it a Pointer problem?
    By officedog in forum C Programming
    Replies: 3
    Last Post: 10-03-2008, 09:01 AM
  2. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. A pointer problem
    By blackswan in forum C Programming
    Replies: 2
    Last Post: 05-03-2005, 06:33 AM
  5. A homework problem about C++ (Pointer)
    By joenching in forum C++ Programming
    Replies: 10
    Last Post: 03-14-2005, 03:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21