運行此代碼沒有段錯誤。評論和打印將給出一些代表。
int main (void)
{
char buffer[11] = {'f', 'e', 'e', 'd', 't', 'h', 'e', 'b', 'a', 'b', 'e'} ;
printf("Buffer's address in the memory:0x%x\n", buffer); //all three are the same
printf("Buffer's address in the memory:0x%x\n", & buffer); //all three are the same
printf("Buffer's address in the memory:0x%x\n", & buffer[0]); //all three are the same
unsigned char a;
for (a=0;a<11;a++)
printf("%c", buffer[a]);
void *temp= (void *)(buffer + 4);
int * size= (int *)temp;
printf ("\ntemp:0x%x\n", temp);
printf ("size:0x%x\n", size);
printf ("size:%c\n", *size);
size = (int *)(buffer + 4);
printf ("\n(int *)*(buffer + 4): 0x%x\n",(int *)*(buffer + 4));
size = *(char *)(buffer + 4);
printf("size:%c\n",size); // 't''s ASCII code
printf("size:0x%x\n",size); // 't'
}
'int * size =(int *)(buffer + 4)'這看起來不像是會導致segfault本身。你想用指針做什麼,爲什麼你不告訴我們這個? –