#include <stdlib.h>
#include <stdio.h>
int main(void)
{
int a[]={1,2,3,4,5};
int b[]={0,0,0,0,0};
int *p;
int i;
p=b;
for(i =0 ; i<5 ; i++)
{
b[i]=a[i]+1;
*p=b[i]-1;
printf(" %i \t %i \t %i \n" ,*p,b[i],a[i]);
}
return 0;
}
我想要的輸出:我無法找到我的程序中的任何錯誤,但它是不會放棄希望的輸出
*p b a
1 2 1
2 3 2
3 4 3
4 5 4
5 6 5
,但它顯示的是這樣的:
*p b a
1 1 1
2 3 2
3 4 3
4 5 4
5 6 5
即使我是在每個[]中添加+1。但是對於第一次打印,它顯示1個輸出。爲什麼?
'* p = b [i] -1;'每次用'b [i] -1'替換'b [0]'的值。所以在第一個使用'i = 0'的循環中,你正在遞減'b [0]'。 – koopajah 2013-03-05 14:36:34
很奇怪http://stackoverflow.com/questions/15224892/c-pointers-and-array必須是一個家庭作業:) – 2013-03-05 14:36:59