指標的運算元順序是由右到左(書上寫的),實際來測試一下
#include<stdio.h> int data[2] = {100,200}; int moredata[2] = {300,400}; int main(void){ int * p1, * p2, * p3; p1 = p2 = data; p3 = moredata; printf(" *p1 = %d, *p2 = %d, *p3= %d\n", *p1, *p2, *p3); printf(" *p1 = %d, *p2 = %d, *p3= %d\n", *p1++, *++p2, (*p3)++); printf(" *p1 = %d, *p2 = %d, *p3= %d\n", *p1, *p2, *p3); //括號測試 printf("It is equal result...\n"); p1 = p2 = data; p3 = moredata; printf(" *p1 = %d, *p2 = %d, *p3= %d\n", *(p1++), *(++p2), (*p3)++); printf(" *p1 = %d, *p2 = %d, *p3= %d\n", *p1, *p2, *p3); return 0; }
*p1 = 100, *p2 = 100, *p3= 300 *p1 = 100, *p2 = 200, *p3= 300 *p1 = 200, *p2 = 200, *p3= 301 It is equal result... *p1 = 100, *p2 = 200, *p3= 301 *p1 = 200, *p2 = 200, *p3= 302
由此得知,*p1++ 相等於 *(p1++)
而不是(*p)++,我之前記錯了><
0 意見:
張貼留言