函式指標,借由傳入不同的參數達到不同的功用
分隔線
/*main.c*/
#include
#include "stdlib.h"
#include "sort.h"
int main(void) {
int number1[] = {3, 5, 1, 6, 9};
sort(number1, 5, larger);
printf("大的在前 ");
int i;
for(i = 0; i < i =" 0;" t =" *a;" a =" *b;" b =" t;"> b;
}
int smaller(int a, int b) {
return a < flag =" 1;" i =" 0;" flag ="="" flag =" 0;" j =" 0;" flag =" 1;" t =" *a;" a =" *b;" b =" t;"> b;
}
int smaller(int a, int b) {
return a < b;
}
void sort(int* arr, int length, CMP compare) {
int flag = 1;
int i, j;
for(i = 0; i < length-1 && flag == 1; i++) {
flag = 0;
for(j = 0; j < length-i-1; j++) {
if(compare(arr[j+1], arr[j])) {
swap(arr + j + 1, arr + j);
flag = 1;
}
}
}
}
/*main.c*/
#include
#include "stdlib.h"
#include "sort.h"
int main(void) {
int number1[] = {3, 5, 1, 6, 9};
sort(number1, 5, larger);
printf("大的在前 ");
int i;
for(i = 0; i < i =" 0;" t =" *a;" a =" *b;" b =" t;"> b;
}
int smaller(int a, int b) {
return a < flag =" 1;" i =" 0;" flag ="="" flag =" 0;" j =" 0;" flag =" 1;" t =" *a;" a =" *b;" b =" t;"> b;
}
int smaller(int a, int b) {
return a < b;
}
void sort(int* arr, int length, CMP compare) {
int flag = 1;
int i, j;
for(i = 0; i < length-1 && flag == 1; i++) {
flag = 0;
for(j = 0; j < length-i-1; j++) {
if(compare(arr[j+1], arr[j])) {
swap(arr + j + 1, arr + j);
flag = 1;
}
}
}
}
struct結構與struct當作函數參數傳遞 by reference
#include
#include
typedef struct {
char color[10];
double radius;
} Ball;
void foo(Ball*);
int main(void) {
Ball ball = {"red", 5.0};
foo(&ball);
printf("ball: %s,\t%.2f\n", ball.color, ball.radius);
system("pause");
return 0;
}
void foo(Ball *ball) { // ball 成員值被複製過來
ball->radius = 100.0;
}
#include
typedef struct {
char color[10];
double radius;
} Ball;
void foo(Ball*);
int main(void) {
Ball ball = {"red", 5.0};
foo(&ball);
printf("ball: %s,\t%.2f\n", ball.color, ball.radius);
system("pause");
return 0;
}
void foo(Ball *ball) { // ball 成員值被複製過來
ball->radius = 100.0;
}
結構中使用函式指標
#include
typedef struct Ball CBall;
struct Ball {
char *color;
double radius;
double (*volumn)(CBall*); // 函式指標
};
// 計算實例體積
double volumn(CBall *this) {
double r = this->radius; // 模擬 this 指標 的行為
return r * r * 3.14;
}
void initBall(CBall *ball, char *color, double radius) {
ball->volumn = volumn; // 連結函式
ball->color = color;
ball->radius = radius;
}
int main(void) {
CBall ball;
initBall(&ball, "red", 5.0);
printf("ball 實例的體積: %.2f\n", ball.volumn(&ball));
return 0;
}
看一看吧!
看不懂的時候可以看這個網頁
http://caterpillar.onlyfun.net/Gossip/CGossip/StructPoint.html
另外,如果在dev-c上面要編這個程式的話是不會compile過的
因為dev-c++
this 是一個關鍵字
我是跑到freeBSD上面用gcc a.c編譯過的
附帶一提,ball->volumn = volumn; // 連結函式
這一行的功能真的只是將這個指標指到 volumn 這個區塊而已,並不會執行
後來進行到 ball.volumn(&ball)
呼叫 ball 的 volumn 指標
才去執行 volumn 這個區塊,並且傳入參數ball
typedef struct Ball CBall;
struct Ball {
char *color;
double radius;
double (*volumn)(CBall*); // 函式指標
};
// 計算實例體積
double volumn(CBall *this) {
double r = this->radius; // 模擬 this 指標 的行為
return r * r * 3.14;
}
void initBall(CBall *ball, char *color, double radius) {
ball->volumn = volumn; // 連結函式
ball->color = color;
ball->radius = radius;
}
int main(void) {
CBall ball;
initBall(&ball, "red", 5.0);
printf("ball 實例的體積: %.2f\n", ball.volumn(&ball));
return 0;
}
看一看吧!
看不懂的時候可以看這個網頁
http://caterpillar.onlyfun.net/Gossip/CGossip/StructPoint.html
另外,如果在dev-c上面要編這個程式的話是不會compile過的
因為dev-c++
this 是一個關鍵字
我是跑到freeBSD上面用gcc a.c編譯過的
附帶一提,ball->volumn = volumn; // 連結函式
這一行的功能真的只是將這個指標指到 volumn 這個區塊而已,並不會執行
後來進行到 ball.volumn(&ball)
呼叫 ball 的 volumn 指標
才去執行 volumn 這個區塊,並且傳入參數ball
不定參數Variable Argument
不定參數
就是傳入函式的參數是不固定的
可多可少
例如printf
int printf(const char *format [,argument]...);
那個中括號括起來的arguement可多可少
而且還可以不同型態
這就是不定參數
可以想像這些不定參數是以stack的方式存起來的
c語言提供va_list可以來實作不定參數的函式
va_start , va_arg , va_end是macro
而va_list是一個char *的型態
va_start做的事就是把va_list的指標指到第一個不定參數
va_arg做的事是以指定型態從va_list取出資料並把va_list指標指到下一個位置
va_end做的事就是當資料取完後把va_list歸零
參考
http://blog.pixnet.net/chiahsiang/post/13125997
http://zhanjun.net/?q=node/23
這篇比較詳細
附上code
/* va_arg example */
#include
#include
#include
void PrintLines ( char* first, ...)
{
char* str;//這個指標是為了在呼叫 va_arg 後,推進指標,用來儲存下一個arg用的
va_list vl;//放置arg陣列的指標名稱
//str=first;//原始code裡面寫的,原本的用途在文章下面會寫
printf("%s\n" , first);//用這一行來取代原始code裡面印出first第一個參數的功能
va_start(vl,first);//初始化
do {
str=va_arg(vl,char*);//這行執行後,str裡面儲存的就是第二個參數的位址了
printf ("%s\n",str);
} while (str!=NULL);
va_end(vl);
}
int main ()
{
PrintLines ("First","Second","Third","Fourth",NULL);
system("pause");
return 0;
}
上面的 code 是從 http://blog.pixnet.net/chiahsiang/post/13125997 複製過來的
不過我有改掉一些地方
因為原本的code為了依序顯示出 first second third fourth
而造成寫法上不易理解 va_start 的前面為什麼要加上 str=first,其實那是不需要的
原始寫法中,先 str=first 之後就可以在do while迴圈中先印出 str 的值
其後再用 va_arg 將下一個參數的位址放到str裡面。
不過這跟在迴圈外面印出 first 意思是一樣的
就是傳入函式的參數是不固定的
可多可少
例如printf
int printf(const char *format [,argument]...);
那個中括號括起來的arguement可多可少
而且還可以不同型態
這就是不定參數
可以想像這些不定參數是以stack的方式存起來的
c語言提供va_list可以來實作不定參數的函式
va_start , va_arg , va_end是macro
而va_list是一個char *的型態
va_start做的事就是把va_list的指標指到第一個不定參數
va_arg做的事是以指定型態從va_list取出資料並把va_list指標指到下一個位置
va_end做的事就是當資料取完後把va_list歸零
參考
http://blog.pixnet.net/chiahsiang/post/13125997
http://zhanjun.net/?q=node/23
這篇比較詳細
附上code
/* va_arg example */
#include
#include
#include
void PrintLines ( char* first, ...)
{
char* str;//這個指標是為了在呼叫 va_arg 後,推進指標,用來儲存下一個arg用的
va_list vl;//放置arg陣列的指標名稱
//str=first;//原始code裡面寫的,原本的用途在文章下面會寫
printf("%s\n" , first);//用這一行來取代原始code裡面印出first第一個參數的功能
va_start(vl,first);//初始化
do {
str=va_arg(vl,char*);//這行執行後,str裡面儲存的就是第二個參數的位址了
printf ("%s\n",str);
} while (str!=NULL);
va_end(vl);
}
int main ()
{
PrintLines ("First","Second","Third","Fourth",NULL);
system("pause");
return 0;
}
上面的 code 是從 http://blog.pixnet.net/chiahsiang/post/13125997 複製過來的
不過我有改掉一些地方
因為原本的code為了依序顯示出 first second third fourth
而造成寫法上不易理解 va_start 的前面為什麼要加上 str=first,其實那是不需要的
原始寫法中,先 str=first 之後就可以在do while迴圈中先印出 str 的值
其後再用 va_arg 將下一個參數的位址放到str裡面。
不過這跟在迴圈外面印出 first 意思是一樣的
訂閱:
文章 (Atom)