我有N个静态分配的结构.
struct exemple{
...
}
struct exemple array[N];
struct exemple *test_ptr = 0x3; /* random address */
Run Code Online (Sandbox Code Playgroud)
我可以检查test_prt是否指向有效地址?即它指向一个"结构示例"分配.
小智 5
你不能.你必须知道.如果正确管理指针,这不是问题.一个良好的习惯是始终设置指针0/ NULL只要你摧毁它们指向的对象.然后你可以用if (ptr)或测试if (!ptr)(或者更详细:if (ptr == NULL)/ if (ptr != NULL)).
请注意您的上次作业
struct exemple *test_ptr = 0x3; /* random address */
Run Code Online (Sandbox Code Playgroud)
是无效的.你不能给指针赋一个整数.但你可以把它投射到指针类型;
struct exemple *test_ptr = (struct exemple *)0x3; /* random address */
Run Code Online (Sandbox Code Playgroud)
结果取决于您的实施/系统.