I want to store a string in memory and read it later:
$$->desc.constant->base.id = (char*)malloc(200);
sprintf($$->desc.constant->base.id, "%f", $1);
printf("->%s\n", $$->desc.constant->base.id); //LINE A
printf("->%i\n", $$->desc.constant); //LINE B
//SOME OTHER CODE
//Then, later on in a function call:
printf("%i", expr->desc.constant); // LINE D
printf("%s", expr->desc.constant->base.id); // LINE C
Although Line B and Line D show the same address, the printf in Line C fails with a Segmentation fault. What am I missing?
Any help would really be appreciated!
printf("->%i\n", $$->desc.constant); //LINE B
That is invalid. As you show the line prior to it that constant is actually a pointer, you cannot treat it as if it were of type int. They don't necassarily have the same sizeof and alignment. Use the format used for void*. It will output memory addresses properly:
printf("->%p\n", (void*)$$->desc.constant); //LINE B
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With