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!
That is invalid. As you show the line prior to it that
constantis actually a pointer, you cannot treat it as if it were of typeint. They don’t necassarily have the same sizeof and alignment. Use the format used forvoid*. It will output memory addresses properly: