Announcement

Collapse
No announcement yet.

Bị lỗi ở tham biến và tham trị

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 08520021
    replied
    Originally posted by clevercoi View Post
    Sao khai báo luôn con trỏ temp thì lại báo lỗi segment default vậy mn?
    => Vì con trỏ temp chưa được cấp phát chứ sao? [MENTION=36419]clevercoi[/MENTION]: nên tìm hiểu một chút trước khi hỏi bạn nhé!

    Leave a comment:


  • clevercoi
    replied
    Originally posted by 08520021 View Post
    Biê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;
    }
    Sao khai báo luôn con trỏ temp thì lại báo lỗi segment default vậy mn?

    Leave a comment:


  • 08520021
    replied
    Originally posted by clevercoi View Post
    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;
    }
    Biê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;
    }
    Last edited by 08520021; 06-11-2013, 21:38.

    Leave a comment:


  • 09520652
    replied
    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;
    }
    Biên dịch lại bằng Visual thì Ok
    Last edited by 09520652; 06-11-2013, 21:16.

    Leave a comment:


  • 10520237
    replied
    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ìa
    Originally posted by clevercoi View Post
    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;
    }

    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;
    }

LHQC

Collapse
Working...
X