#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Currently Poland, sometimes London, originally Wales . . .
    Posts
    154
    Rep Power
    11

    Passing structure pointer by reference to function


    Hi,

    Let me start by saying that I am relatively new to the topic of C/C++ and so please excuse my lack of exact terminology.

    I am developing a small program which focuses on a linked list of structures. This is being done as a learning exercise.

    I am looking to separate out the code into functions. Although I'm quite familar with functions in general, I am getting a bit lost regarding how to create a function to add a node to a linked list.

    I have had no problems in defining a structure and using pointers to build a linked list and to allocate/deallocate memory for nodes within the list.

    I would now like to add a function that will add a node to the linked list and populate it with data. Data is being read from a file and will be passed as arguments to the function. The function should operate on data within itself in such a way that the result of the operation are seen outside of the function.

    I have seen many example functions whereby two integers are defined within the main function and passed by reference to a function such that the values of the integers are seen outside of the function without the function returning values.

    It is this same technique that I would like to apply to my linked list program. I believe I might be able to get my head round it if I were merely dealing with structures directly as I could then pass them by reference to a function, however as I am dealing with the linked list via pointers only I just can't seem to get the hang of it.

    I'll try and explain clearly what I have and what I am trying to achieve and I would greatly appreciate it if someone could point me in the right direction - I'm not looking for a full solution (that would take the fun out of the learning process), just some short examples and suggestions.

    I'll not post the exact structure and pointer names I'm using and try to keep things generic.

    Imagine that I have the following:

    1. A structure of the type:
    struct node {
    int value;
    node* nextNode;
    };

    2. Pointers for navigating the list:
    node* startPointer = NULL;
    node* currentPointer = startPointer;

    These are initalised as NULL so that I can tell whether any nodes have yet been initialised.

    I would normally allocate memory for a new node and populate it as follows:
    currentPointer->nextNode = new node;
    currentPointer=currentPointer->nextNode;
    currentPointer->value=31;

    I would like to achieve the same as the above three lines by via a function and without having to return values. And that's about it.

    Any ideas?
  2. #2
  3. No Profile Picture
    Principal Goatkeeper
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Location
    Galway, Ireland
    Posts
    296
    Rep Power
    179
    Something like this ?

    Code:
    void AddNode(node **current, int node_value){
    *(current)->nextNode = new node;
    *(current)=*(current)->nextNode;
    *(current)->value=node_value;
    }
    
    
    
    //later....
    AddNode(&currentPointer, 31);
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Currently Poland, sometimes London, originally Wales . . .
    Posts
    154
    Rep Power
    11
    Thanks very much!

    I'll give it a shot soon and see what happens, however it certainly looks to be very much on the right track.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2003
    Location
    Currently Poland, sometimes London, originally Wales . . .
    Posts
    154
    Rep Power
    11
    I've just given it a go and it works a treat.

    I had previously failed to use two stars in the definition of the first argument (only one star) and had not been referencing values correctly inside my function.

    With this example, I've managed to see that it can work correctly.

    Thanks again for your help - possibly a simple issue, but one that chose to evade me!

IMN logo majestic logo threadwatch logo seochat tools logo