我想在内存中存储一个字符串并稍后阅读:
$$->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
Run Code Online (Sandbox Code Playgroud)
尽管B行和D行显示相同的地址,但C行中的printf失败并出现Segmentation故障.我错过了什么?
真的很感激任何帮助!