Re: [問題] 指向函式的指標語法..
※ 引述《oit73 (我需要"激勵")》之銘言:
: 以下是我練習--指向函式的指標語法
: 目的→x,y值 互換
: 想請教下面是哪出問題...x,y一直沒辦法互換
: (一直沒辦法學好指標耶..)
: <<程式開始>>
: #include <stdio.h>
: #include <stdlib.h>
: float call(float x, float y , float(*ptr)(float,float)){
: return (*ptr)(x,y);
: }
: float fun(float x, float y){
: float temp;
: temp = x;
: x = y;
: y = temp;
: return x,y;
: }
: int main(){
: float x,y;
: printf("x = ");scanf("%f", &x);
: printf("y = ");scanf("%f", &y);
: printf("\n\n");
: call(x,y,fun);
: printf("對調後, x = %f\n", x);
: printf(" , y = %f", y);
: system("pause");
: }
float call(float *x, float *y , float(*ptr)(float *,float *)){
return (*ptr)(x, y);
}
float fun(float *x, float *y){
float temp;
temp = *x;
*x = *y;
*y = temp;
}
int main()
{
float x,y;
printf("x = ");scanf("%f", &x);
printf("y = ");scanf("%f", &y);
printf("\n\n");
call(&x,&y,fun);
printf("對調後, x = %.1f\n", x);
printf(" , y = %.1f", y);
system("pause");
}
感謝推文指正,果然沒有 call by address,一直都是 call by value
--
PHP MVC CodeIgniter 繁體中文手冊: http://tinyurl.com/mduyv8
PHP MVC CodeIgniter 繁體中文討論區:http://tinyurl.com/yayzoz8
PHP MVC CodeIgniter 中文官方網站: http://tinyurl.com/yatds8n
Appleboy Blog 電腦技術: http://blog.Wu-Boy.com
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.123.107.202
推
02/10 18:43, , 1F
02/10 18:43, 1F
推
02/10 19:03, , 2F
02/10 19:03, 2F
推
02/11 01:14, , 3F
02/11 01:14, 3F
※ 編輯: appleboy46 來自: 140.123.107.202 (02/11 12:17)
→
02/11 12:17, , 4F
02/11 12:17, 4F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章