Thread: Pointer to struct

  1. #1
    Registered User
    Join Date
    Jan 2020
    Posts
    6

    Pointer to struct

    Hello everyone, l have following code and l see an issue wich l cant understand. When l call the printInfo() method its showing two different things of the same struct. Is located at the second call of
    Code:
    printInfo(*table1.next);
    so my question is why is happening this?
    Code:
    #pragma hdrstop
    #pragma argsused
    #include <stdlib.h> // rand(), random(int), randomize(), malloc(), calloc()
    
    typedef struct _Node {
       int id;
       struct _Node *next;
    } node;
    
    void  printInfo (node  _N);
    void  insert    (node *_N, int _id);
    void  initialize(node *_N);
    char* checkPtr  (node *_ptr);
    
    int main(int argc, char* argv[]) {
       node table1;
    
       initialize(&table1);
       insert(&table1, 10);
    
       printInfo(*table1.next); // okay
       printInfo(table1); // okay
       printInfo(*table1.next); // whats going on?
    
       getch();
       return 0;
    }
    
    void insert(node *_N, int _id) {
       node temp;
    
       temp.id = _id;
       temp.next = NULL;
    
       _N->next = &temp;
    };
    
    void printInfo (node _N) {
       printf("   id = %d\n", _N.id);
       printf(" next = %s\n\n", checkPtr(_N.next));
    };
    
    void initialize (node *_N) {
       _N->id = 0;
       _N->next = NULL;
    };
    
    char* checkPtr (node *_ptr) {
       return _ptr==NULL?"NULL":"NOT NULL";
    };
    This is how is taken form
    Pointer to struct-untitled-png

    the output is
    Code:
       id = 10
     next = NULL
    
       id = 0
     next = NOT NULL
    
    // why is this?
       id = 4202672 
     next = NOT NULL
    Last edited by vincentThorpe; 01-29-2020 at 04:34 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,550
    The problem lies here:
    Code:
    void insert(node *_N, int _id) {
       node temp;
     
       temp.id = _id;
       temp.next = NULL;
     
       _N->next = &temp;
    };
    temp is a local variable, so &temp is the address of a local variable. Trouble is, after control leaves this function, temp no longer exists, but the next pointer still exists, which means that the next pointer would then contain an invalid address. Basically, this is why nodes for linked lists are often created with malloc instead: you want the new node to persist beyond the lifetime of the function that does the insertion.

    Incidentally, I suggest that printInfo have a pointer to const node parameter instead: passing a node by value is potentially expensive because the node data can be potentially expensive to copy (although it isn't in this case).

    Also, you should get rid of the semi-colon after the closing brace.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2020
    Posts
    6
    Thank you for the effort. I agree with that `node temp;` dies after `insert()` is done but then this doesnt explain why in the first call of `printInfo()` struct temp can be accessed. But l think l can live with that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 06-27-2012, 07:05 AM
  2. Replies: 1
    Last Post: 05-12-2011, 01:02 AM
  3. 2 problems: Struct array and pointer + struct
    By v1n1c1u5 in forum C Programming
    Replies: 0
    Last Post: 12-13-2009, 05:38 PM
  4. Replies: 1
    Last Post: 05-05-2004, 06:58 AM
  5. returning a pointer of a struct of a struct array...
    By myrddinb in forum C Programming
    Replies: 1
    Last Post: 04-13-2004, 06:49 PM

Tags for this Thread