Originally posted by clevercoi
View Post
Announcement
Collapse
No announcement yet.
Bị lỗi ở tham biến và tham trị
Collapse
X
-
-
Originally posted by 08520021 View PostBiên dịch bằng gcc bị lỗi thế là đúng rồi, bắt lỗi tí nhé:
- hoanvi # convert
- line 3: C cho phép param kiểu &x? (lỗi compiler bắt)
- khai báo biến trùng tên.
- hàm convert kiểu void return 0?
Code:#include<stdio.h> void swap(int *x, int *y){ int temp; temp = *x; *x = *y; *y = temp; } int main(){ int a,b; printf("Input a,b \n"); scanf("%d%d",&a, &b); swap(&a, &b); printf("%d\t%d\n", a, b); return 0; }
Leave a comment:
-
Originally posted by clevercoi View PostTrình biên dịch cứ báo lỗi này. Tại vì mình nghĩ là tham biến nên khai báo như vậy nhưng gcc vẫn báo lỗi.
error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
Code:#include<stdio.h> void convert(int &x, int &y){ int x,y,temp; temp = x; x = y; y = temp; return 0; } int main(){ int a,b; printf("Input a,b \n"); scanf("%d%d",&a,&b); hoanvi(&a, &b); printf("%d\t%d\n",a,b); return 0; }
- hoanvi # convert
- line 3: C cho phép param kiểu &x? (lỗi compiler bắt)
- khai báo biến trùng tên.
- hàm convert kiểu void return 0?
Code:#include<stdio.h> void swap(int *x, int *y){ int temp; temp = *x; *x = *y; *y = temp; } int main(){ int a,b; printf("Input a,b \n"); scanf("%d%d",&a, &b); swap(&a, &b); printf("%d\t%d\n", a, b); return 0; }
Last edited by 08520021; 06-11-2013, 21:38.
Leave a comment:
-
Lỗi:
- Biến đã khởi tạo rồi thì trong hàm hoán vị không cần khai báo lại
- hàm kiểu void mà lại còn return 0;
- Trên ghi hàm convert() dưới gọi hàm hoanvi() @@
- Trong hàm hoanvi, biến đầu vào không cần ghi &
==> Chú này hổng kiến thức cấp độ nặng, đọc lại sách đi thôi.:sexy:
Code:#include<stdio.h> void hoanvi(int &x, int &y){ int temp; temp = x; x = y; y = temp; } int main(){ int a,b; printf("Input a,b \n"); scanf("%d%d",&a,&b); hoanvi(a, b); printf("%d\t%d\n",a,b); return 0; }
Last edited by 09520652; 06-11-2013, 21:16.
Leave a comment:
-
truyền tham chiếu 2 biến x, vào hàm thì sao còn khai bao 2 biến x y trong hàm nữa. lỗi nằm chỗ khai báo x y trong hàm kìaOriginally posted by clevercoi View PostTrình biên dịch cứ báo lỗi này. Tại vì mình nghĩ là tham biến nên khai báo như vậy nhưng gcc vẫn báo lỗi.
error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
Code:#include<stdio.h> void convert(int &x, int &y){ int x,y,temp; temp = x; x = y; y = temp; return 0; } int main(){ int a,b; printf("Input a,b \n"); scanf("%d%d",&a,&b); hoanvi(&a, &b); printf("%d\t%d\n",a,b); return 0; }
Leave a comment:
-
Bị lỗi ở tham biến và tham trị
Trình biên dịch cứ báo lỗi này. Tại vì mình nghĩ là tham biến nên khai báo như vậy nhưng gcc vẫn báo lỗi.
error: expected ‘;’, ‘,’ or ‘)’ before ‘&’ token
Code:#include<stdio.h> void convert(int &x, int &y){ int x,y,temp; temp = x; x = y; y = temp; return 0; } int main(){ int a,b; printf("Input a,b \n"); scanf("%d%d",&a,&b); hoanvi(&a, &b); printf("%d\t%d\n",a,b); return 0; }
Tags: None
Leave a comment: