目前按照書本上面做了一個程式:
#include<stdio.h>
#include<stdlib.h>
int swap();
main()
{
int x=10,y=20;
printf("%d-%d\n",x,y);
swap(x,y);
printf("%d-%d\n",x,y);
system("PAUSE");
}
int swap(int &x,int &y)
{
int temp=0;
temp=x;
x=y;
y=temp;
}
程式的目的是要將X,Y的值做對調,但是我使用的Dev-C++ 4.9.9.2編譯的時候有問題,但是把&拿走就可以,可是這是書中的範例,所以我想詢問這部份的問題出在哪?
謝謝.
hsg210 wrote:
你把編譯器說的錯誤是...(恕刪)
專案我確定是開C的啦.
錯誤訊息如下:
4 C:\Dev-Cpp\main.c syntax error before '&' token
15 C:\Dev-Cpp\main.c syntax error before '&' token
C:\Dev-Cpp\main.c In function `swap':
18 C:\Dev-Cpp\main.c `x' undeclared (first use in this function)
(Each undeclared identifier is reported only once
for each function it appears in.)
19 C:\Dev-Cpp\main.c `y' undeclared (first use in this function)
C:\Dev-Cpp\Makefile.win [Build Error] [main.o] Error 1
果然還是不能過
(樓上怎麼過的??????)
感覺怪怪的
發現你的程式很怪
怎麼會用&當作宣告point的呢???????
應該是用*吧
語法有問題 這是課本的?
而且你這樣換也沒用swap又沒有真正改到X Y
你在main裡面printf 是白印
只有在swap裡面換爽的
我改了一下
----------------------------
#include<stdio.h>
#include<stdlib.h>
void swap(int*,int*);
int main(void)
{
int x=10,y=20;
printf("%d-%d\n",x,y);
swap(&x,&y);
printf("%d-%d\n",x,y);
system("PAUSE");
return 0;
}
void swap(int *x,int *y)
{
int temp=0;
temp=*x;
*x=*y;
*y=temp;
}
這樣就可以了
可以看到真正有交換X Y
我想你把課本的範例打錯了吧
大學新生???
還是高中生???
內文搜尋

X