In first print temp->data
is address
whereas in later print temp->data
becomes value
and *temp->data
prints nothing
I have tried assining
value like
int n = 5;
temp->data = &n;
then *temp->data prints value 5
structure is
struct Node{
int *data;
struct Node *next;
};
/***part of code ***/
struct Node *temp = NULL;
temp = head;
while(count != l){
count++;
printf("%x %x %x ",temp,temp->data,&(temp->data));
printf("enter %d element\n",count);
scanf("%d",&(temp->data));
printf("%d\n",temp->data);
int *data;
creates an unintialized pointer and must point to valid memory before it can be used. (failure to do so leads to Undefined Behavior, and likely a SegFault)
For example:
temp->data = malloc (sizeof *temp->data);
if (temp->data == NULL) {
perror ("malloc-temp->data");
/* handle error, e.g. return/exit */
}
Now you can assign an integer value to temp->data
. (don't forget to free
the memory you allocate when it is no longer needed)
Presuming you have done that (in some other code you didn't post), the following is wrong:
scanf("%d",&(temp->data));
temp->data
is already a pointer, no '&'
is needed before it.
(you must ALWAYS validate the scanf
return before you consider your input valid)
&temp->data
creates a pointer-to-pointer-to int
(e.g. the address of the pointer to data
). Printing pointer values with %x
leads to undefined behavior unless sizeof (a_pointer)
and sizeof (unsigned)
are the same on your system (not for x86_64). Use %p
to print addresses.
Address those issues and let me know if you have further problems.