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!