試圖更好地學習指針數學我寫了這段代碼。其目的是增加指針投擲結構和打印它的成員。我知道如何打印成員更簡單的方法,但真的想知道我的指針數學是如何搞砸的。謝謝。C指針數學結構
typedef struct{
int num;
int num2;
char *string;
} astruct ;
int main (int argc, const char * argv[])
{
astruct mystruct = { 1234, 4567,"aaaaaaa"};
astruct *address;
address = &mystruct;
// this does print 1234
printf("address 0x%x has value of:%i\n",address, *address);
address = address + sizeof(int);
//this does NOT print 4567
printf("address 0x%x has value of:%i\n",address, *address);
address = address + sizeof(int);
//this crashes the program, I wanted to print aaaaaaaa
printf("address 0x%x has value of:%s\n",address, **address);
return 0;
}
並不能保證這兩個整數至少是連續的嗎? – Alnitak 2011-04-09 15:12:31
該標準使得關於填充的mo保證。 (這就是所有*可能* s的原因) – hugomg 2011-04-09 19:22:10