yes the value data in node will be the same as the data in x as you said , why didn't
you test it, just write before the end of the main, write these 2 lines to make sure and don't forget to include <stdio.h> haeder file:
printf("node date : %d\n", node->data);
printf("x date : %d\n", x->data);
and here is the output :
node date : 41
x date : 41
notice that the value 41 is just the ASCII representation of the char )
the line node->data = x->data; is equivalent to saying (*node).data = (*x).data; ,
which is just referring to the place of variable data and assigning its place to another place in the memory called also data but belongs to another instance of that struct.
also you have a small problem in line x->data = y; , this should give you warning as lValue is int while the rValue is char and here is the warning that my compiler gives me:
Clang-Tidy: 'signed char' to 'int' conversion; consider casting to 'unsigned char' first
so you should cast this value , so do x->data = (int)y; not x->data = y;
and one last thing , you should always check for the value returned from malloc function as it may return NULL if it couldn't allocate that memory for you in the heap , so it's a good practice to if(node != NULL && x != NULL) { . . .}
and here is the full code to test it:
#include <stdlib.h>
#include <stdio.h>
typedef struct node_tag{
int data;
struct node_tag* next;
}NODE;
int main() {
char y = ')';
NODE *x = (NODE *) malloc(sizeof(NODE));
NODE *node = (NODE *) malloc(sizeof(NODE));
/*always check if malloc could allocate tha memory for you*/
if(x != NULL && node != NULL)
{
//initialization
x->data = (int)y;
x->next = NULL;
node->next = NULL;
// test 1
node->data = x->data;
printf("node date test(1) : %d\n", node->data);
printf("x date test(1) : %d\n", x->data);
//test 2
x->data = 25;
(*node).data = (*x).data;;
printf("node date test(2) : %d\n", node->data);
printf("x date test(2) : %d\n", x->data);
}
return 0;
}
and here is the output:
node date test(1) : 41
x date test(1) : 41
node date test(2) : 25
x date test(2) : 25