This is the def of my structure
typedef struct treeNode
{
int data,pos;
char name[16];
struct treeNode *left;
struct treeNode *right;
}treeNode;
I have created an dynamic object
treeNode *temp;
temp = (treeNode *)malloc(sizeof(treeNode));
If I have to assign a value to data how should I assign
scanf("%d",temp->data); //or
scanf("%d",&(temp->data)); //why? because all scanf will look for is address to a location which could be done by temp->data;
and this goes for accessing data also i.e. how should I access the integer part ?
temp->data; //or
*(temp->data)
scanf("%d",&temp->data);Because scanf() needs address of variable. For each conversion specifier, scanf() expects the corresponding argument to be a pointer to the proper type: %d expects an argument of type int *, %f expects an argument of type double *, %c and %s both expect an argument of type char *, etc.
Behaviour of scanf() gives more idea about scanf().
for accessing
temp->datais enough, because it is essentially equivalent to(*temp).data.If you try to access
*(temp->data)then you trigger undefined behaviour. Because, you are accessing a memory location which may be in other process’s context.say temp->data is 100, then *(temp->data) means *(100), i.e accessing at memory location 100.