Like Tree3Likes
  • 3 Post By Salem

Initializing structure

This is a discussion on Initializing structure within the C Programming forums, part of the General Programming Boards category; I have this code which does not print "3" as it is supposed to Code: #include<stdio.h> #include<stdlib.h> typedef struct Node ...

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    24

    Initializing structure

    I have this code which does not print "3" as it is supposed to
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    typedef struct Node
    {
    int i;
    }Node, *NodePtr;
    
    
    void Init(NodePtr mynode)
    {
    mynode=(NodePtr)malloc(sizeof(Node));
    }
    
    
    void Insert(NodePtr mynode, int i)
    {
    mynode->i=i;
    }
    
    
    void Retrieve(NodePtr mynode)
    {
    printf("%d",mynode->i);
    }
    
    
    void UnInit(NodePtr mynode)
    {
    free(mynode);
    }
    
    
    int main()
    {
    NodePtr mynode;
    Init(mynode);
    Insert(mynode,3);
    Retrieve(mynode);
    UnInit(mynode);
    
    
    return 0;
    }
    But when I initialize the structure in main, it works(check the following code)
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    
    typedef struct Node
    {
    int i;
    }Node, *NodePtr;
    
    
    void Insert(NodePtr mynode, int i)
    {
    mynode->i=i;
    }
    
    
    void Retrieve(NodePtr mynode)
    {
    printf("%d",mynode->i);
    }
    
    
    void UnInit(NodePtr mynode)
    {
    free(mynode);
    }
    
    
    int main()
    {
    NodePtr mynode;
    mynode=(NodePtr)malloc(sizeof(Node));
    Insert(mynode,3);
    Retrieve(mynode);
    UnInit(mynode);
    
    
    return 0;
    }
    Why does the first code not work? Is there a way to initialize structure by calling a function?
    Thanks!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Do you understand why this doesn't print 3
    Code:
    void foo ( int bar ) {
      bar = 3;
    }
    int main ( ) {
      int x = 0;
      foo(x);
      printf("%d\n", x);
      return 0;
    }
    When you understand that you need x=foo(); or foo(&x); to make this work, you'll know how to fix your code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2012
    Posts
    24
    I know why your code doesn't print. But I am passing the structure address only. So why wouldn't it print the value?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > But I am passing the structure address only. So why wouldn't it print the value?
    Ah, but you aren't are you.

    First thing to do is to stop typedef'ing pointer types.
    It really doesn't help to clear things up for you.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    typedef struct Node
    {
        int i;
    }Node;
    
    void Init(Node *mynode)
    {
        mynode=malloc(sizeof(Node));
    }
    
    int main()
    {
        Node *mynode = NULL;
        Init(mynode);
    }
    It may be a pointer, but you're still passing it "by value", so changing it inside Init won't change it in main.
    AndiPersti, Click_here and qny like this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    It does not matter at all what type the thing you want to modify is, if you want to modify it through a function parameter, you need to pass a pointer to it. Even if that thing is itself a pointer!

    As it is, your function could only modify what mynode already points at, but cannot be made to point at anything else; and as it currently points to nothing, it shall stay pointing at nothing.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    24
    So how do I pass it by reference?

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    24
    Okay! I got it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-26-2011, 10:40 PM
  2. initializing structure in a class
    By diego in forum C++ Programming
    Replies: 4
    Last Post: 05-20-2010, 08:40 AM
  3. initializing structure variables
    By cs32 in forum C Programming
    Replies: 2
    Last Post: 04-11-2008, 05:33 PM
  4. Initializing a reference within a structure
    By Russell in forum C++ Programming
    Replies: 3
    Last Post: 11-30-2005, 06:22 AM
  5. initializing nested structure arrays
    By linucksrox in forum C Programming
    Replies: 2
    Last Post: 06-10-2004, 10:58 PM

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