vi /etc/csh.login
# 支援中文的環境,讓我們可以在 console 下或 telnet 中使用中文。
setenv LANG zh_TW.Big5
結構中使用函式指標
#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
函式指標
─────────────分隔線───────────────
/*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 < 5; i++) {
printf("%d ", number1[i]);
}
putchar('\n');
int number2[] = {3, 5, 1, 6, 9};
sort(number2, 5, smaller);
printf("小的在前 ");
for(i = 0; i < 5; i++) {
printf("%d ", number2[i]);
}
putchar('\n');
system("pause");
return 0;
}
─────────────分隔線───────────────
/*sort.h*/
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int larger(int a, int b) {
return a > b;
}
int smaller(int a, int b) {
return a < b;
}
void sort(int* arr, int length, int (*compare)(int, int)) {
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;
}
}
}
}
─────────────分隔線───────────────
sort.h 裡面的 compare 是一個指標
可以接受 main.c 呼叫他的時候給予不同的名稱,如 larger 或者 smaller
感覺就像是一個 char 指標指向不同的記憶體區塊,而獲得不同的內容
也就是說
sort(number1 , 5 , larger)
同於
sort(int* arr, int length, int larger(int, int))
/*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 < 5; i++) {
printf("%d ", number1[i]);
}
putchar('\n');
int number2[] = {3, 5, 1, 6, 9};
sort(number2, 5, smaller);
printf("小的在前 ");
for(i = 0; i < 5; i++) {
printf("%d ", number2[i]);
}
putchar('\n');
system("pause");
return 0;
}
─────────────分隔線───────────────
/*sort.h*/
void swap(int *a, int *b) {
int t = *a;
*a = *b;
*b = t;
}
int larger(int a, int b) {
return a > b;
}
int smaller(int a, int b) {
return a < b;
}
void sort(int* arr, int length, int (*compare)(int, int)) {
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;
}
}
}
}
─────────────分隔線───────────────
sort.h 裡面的 compare 是一個指標
可以接受 main.c 呼叫他的時候給予不同的名稱,如 larger 或者 smaller
感覺就像是一個 char 指標指向不同的記憶體區塊,而獲得不同的內容
也就是說
sort(number1 , 5 , larger)
同於
sort(int* arr, int length, int larger(int, int))
不定參數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 意思是一樣的
Innd 安裝教學
轉自【新客網FreeBSD教程】 INN安裝步驟小結
由magicalloveshe排版編修
1、
用vipw修改/etc/passwd文件,改為
news:*:8:8:News Subsystem:/usr/local/news:/bin/sh
2、
$tar zxvf inn-2.4.0.tar.gz
$cd inn-2.4.0
$./configure
$make && make install
預設安在/usr/local/news下
3、
修改etc/inn.conf
hiscachesize: 256
artcutoff: 60
maxartsize: 100000
localmaxartsize: 100000
enableoverview: true
ovmethod: tradindexed
spoolfirst: false
執行 /usr/local/news/bin/inncheck , 檢查inn.conf
4、文章過期策略設置修改expire.ctl如下:
##This entry uses the syntax appropriate when groupbaseexpiry is true in
inn.conf.
##This is an entry based on storage class, used when groupbaseexpiry is flase.
#0:10:never:never
5、初始化history文件
$cd /usr/local/news/db
$touch history
$../bin/makedbz -i
$for i in history.n*; do i=${i#history.n.};mv history.n.$i history.$i ;done
有的shell執行不了,也可以手工改正 (起的作用是把history.n.*改名為history.*)
$chown news:news history*
$chmod 644 history*
6、建立新的新聞組
su - news
/usr/local/news/bin/ctlinnd newgroup test1
/usr/local/news/bin/ctlinnd newgroup test2
要執行ctlinnd必須要先啟動innd
請先跳到第十項閱讀如何啟動innd
當然了,你可以試試如果不啟動能不能使用ctlinnd
7、
用mkuserdb.pl建立userpass文件,userpass文件一般存放在/usr/local/news/db下
magicallove:mkuserdb.pl在哪裡?
因為找不到mkuserdb.pl在哪裡,所以這項其實我沒有做
8、修改storage.conf文件,加入
method tradspool {
newsgroups: *
class: 1
}
magicallove:/usr/local/news/etc/storage.conf
這份storage.conf裡面原本就有
method tradspool {
newsgroups: *
class: 0
}
不知道是要修改還是新增在下面= =
9、修改readers.conf,格式如下
auth admin {
hosts: *
auth: "ckpasswd -f /usr/local/news/db/userpass"
default:
default-domain:
}
下面這塊在原本的文件中就存在囉
auth "localhost" {
hosts: "localhost, 127.0.0.1, stdin"
default: ""
}
access user1 {
users: "zhenhua@, !@"
newsgroups: "test2,!control.*,!junk,!control"
access: RP
}
access user2 {
users: "yingjia@, !@"
newsgroups: "test1,test2,!control.*,!junk,!control"
access: RP
}
下面這塊在原本的文件中就存在囉
access "localhost" {
users: ""
newsgroups: "*"
access: RPA
}
access fail {
users: "@*"
newsgroups: !*
}
配置syslog,記錄日誌
$ touch /usr/local/news/log/news.crit
$ touch /usr/local/news/log/news.err
$ touch /usr/local/news/log/news.notice
$ chown news /usr/local/news/log/news.*
$ chgrp news /usr/local/news/log/news.*
$ cat >> /etc/syslog.conf << style="color: rgb(0, 0, 153);">原本沒有log這個資料夾,請自己新增哦
cat >> /etc/syslog.conf <<>
這行需要一點串流概念,建議到鳥哥上面看完那份bash教學
10、啟動news服務:
$su - news
$/usr/local/news/bin/rc.news
netstat -an | grep LISTEN命令查看119 port是否開啟
11、用outlookexpress測試新聞群組
yingjia可以訂閱test1,test2
zhenhua只能訂閱test2
12.系統啟動時,自動啟動新聞組服務器確定
/etc/rc中有執行rc.local的部分,然後在rc.local(如沒有,則新建)中加入:
su news -c /usr/local/news/bin/rc.news
由magicalloveshe排版編修
1、
用vipw修改/etc/passwd文件,改為
news:*:8:8:News Subsystem:/usr/local/news:/bin/sh
2、
$tar zxvf inn-2.4.0.tar.gz
$cd inn-2.4.0
$./configure
$make && make install
預設安在/usr/local/news下
3、
修改etc/inn.conf
hiscachesize: 256
artcutoff: 60
maxartsize: 100000
localmaxartsize: 100000
enableoverview: true
ovmethod: tradindexed
spoolfirst: false
執行 /usr/local/news/bin/inncheck , 檢查inn.conf
4、文章過期策略設置修改expire.ctl如下:
##This entry uses the syntax appropriate when groupbaseexpiry is true in
inn.conf.
##This is an entry based on storage class, used when groupbaseexpiry is flase.
#0:10:never:never
5、初始化history文件
$cd /usr/local/news/db
$touch history
$../bin/makedbz -i
$for i in history.n*; do i=${i#history.n.};mv history.n.$i history.$i ;done
有的shell執行不了,也可以手工改正 (起的作用是把history.n.*改名為history.*)
$chown news:news history*
$chmod 644 history*
6、建立新的新聞組
su - news
/usr/local/news/bin/ctlinnd newgroup test1
/usr/local/news/bin/ctlinnd newgroup test2
要執行ctlinnd必須要先啟動innd
請先跳到第十項閱讀如何啟動innd
當然了,你可以試試如果不啟動能不能使用ctlinnd
7、
用mkuserdb.pl建立userpass文件,userpass文件一般存放在/usr/local/news/db下
magicallove:mkuserdb.pl在哪裡?
因為找不到mkuserdb.pl在哪裡,所以這項其實我沒有做
8、修改storage.conf文件,加入
method tradspool {
newsgroups: *
class: 1
}
magicallove:/usr/local/news/etc/storage.conf
這份storage.conf裡面原本就有
method tradspool {
newsgroups: *
class: 0
}
不知道是要修改還是新增在下面= =
9、修改readers.conf,格式如下
auth admin {
hosts: *
auth: "ckpasswd -f /usr/local/news/db/userpass"
default:
default-domain:
}
下面這塊在原本的文件中就存在囉
auth "localhost" {
hosts: "localhost, 127.0.0.1, stdin"
default: "
}
access user1 {
users: "zhenhua@
newsgroups: "test2,!control.*,!junk,!control"
access: RP
}
access user2 {
users: "yingjia@
newsgroups: "test1,test2,!control.*,!junk,!control"
access: RP
}
下面這塊在原本的文件中就存在囉
access "localhost" {
users: "
newsgroups: "*"
access: RPA
}
access fail {
users: "
newsgroups: !*
}
配置syslog,記錄日誌
$ touch /usr/local/news/log/news.crit
$ touch /usr/local/news/log/news.err
$ touch /usr/local/news/log/news.notice
$ chown news /usr/local/news/log/news.*
$ chgrp news /usr/local/news/log/news.*
$ cat >> /etc/syslog.conf << style="color: rgb(0, 0, 153);">原本沒有log這個資料夾,請自己新增哦
cat >> /etc/syslog.conf <<>
這行需要一點串流概念,建議到鳥哥上面看完那份bash教學
10、啟動news服務:
$su - news
$/usr/local/news/bin/rc.news
netstat -an | grep LISTEN命令查看119 port是否開啟
11、用outlookexpress測試新聞群組
yingjia可以訂閱test1,test2
zhenhua只能訂閱test2
12.系統啟動時,自動啟動新聞組服務器確定
/etc/rc中有執行rc.local的部分,然後在rc.local(如沒有,則新建)中加入:
su news -c /usr/local/news/bin/rc.news
訂閱:
文章 (Atom)