说,给定结构:
struct someStruct {
unsigned int total;
};
struct someStruct s; // initiate an instance (allocate memory)
s.total = 5555; // set a value
// and for
void* cmd; // local holder, which is a pointer (may be an argument of a function)
// at some given time
// format the designated pointer with a struct form(at), 'casting' the pointer
struct someStruct *cmd_ptr = (struct someStruct *) cmd;
cmd = &s; // pass the specific address of the allocated structure and space to the pointer
我们如何显示 cmd.total 值?这些都不起作用。
// retrieve the data
//printf(" Struct contents: %d \n", (cmd->total)); // use designated pointer
//printf(" Struct contents: %d \n", (*cmd).total); // use designated pointer
//printf(" Struct contents: %d \n", cmd.total); // use specific address
//printf(" Struct contents: %d \n", (&cmd).total); // use designated pointer
//printf(" Struct contents: %d \n", (&cmd)->total); // use designated pointer