1

与えられた構造体を言う:

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
4

3 に答える 3

1

印刷に cmd を使用しています。cmdのタイプを変更するstruct someStruct *か、タイプキャストします。

void ポインターには型がないため、必要なフィールドにアクセスするためにポインター演算を実行する方法がわかりません。

于 2013-10-03T07:04:33.670 に答える
0

cmdsomeStructにキャストバックする必要があります。

((struct someStruct*)cmd)->total
于 2013-10-03T07:05:06.003 に答える