[問題] function定義後接著struct/int ?

看板C_and_CPP作者 (autumned)時間12年前 (2011/12/20 22:55), 編輯推噓5(5022)
留言27則, 13人參與, 最新討論串1/1
如題,在寫網路模擬的作業,助教給的code 是.c 檔 compile下去一大桶error 但是我有看過程式開頭的註解 確定這個檔不是要學生填空的 而且還警告學生不可以改這個檔的內容... 裡面有很多error是來自於這種函式定義 void InsertEvent(p) struct event *p; { // blah blah blah } /* END OF InsertEvent */ void stoptimer(AorB) int AorB; /* A or B is trying to stop timer */ { //blah blah blah } 請問 這是什麼語法...我完全沒有印象有看過這種函式定義和{}中間夾著東西 的寫法 .. (constructor除外..)QQ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.231.96.222

12/20 22:59, , 1F
等同於把 struct event *p 寫在 小括號的裡面。
12/20 22:59, 1F

12/20 22:59, , 2F
古早的c語言有用這種宣告方式,現在用gcc應該是可以編譯
12/20 22:59, 2F
void InsertEvent(p) struct event *p; { // blah blah blah } /* END OF InsertEvent */ == void InsertEvent(struct event *p) { // blah blah blah } /* END OF InsertEvent */ void stoptimer(AorB) int AorB; /* A or B is trying to stop timer */ { //blah blah blah } == void stoptimer(int AorB) { //blah blah blah } 請問是這個意思嗎@@ ※ 編輯: autumned 來自: 61.231.96.222 (12/20 23:03)

12/20 23:02, , 3F
這叫 K&R style 的函式定義,其實就是把參數型別寫後面
12/20 23:02, 3F

12/20 23:03, , 4F
這種寫法已經是化石級的code,新標準C99/C++98都不吃的
12/20 23:03, 4F

12/20 23:08, , 5F
QQ 那我應該自己把他們都改回去 還是去找個可以邊的compi
12/20 23:08, 5F

12/20 23:08, , 6F
ler ... @@
12/20 23:08, 6F
裡面還有這種 .... @@ 401 C:\Users\user\Desktop\HW3\project3.c aggregate `timeval tp' has incomplete type and cannot be defined 403 C:\Users\user\Desktop\HW3\project3.c `gettimeofday' undeclared (first use this function) void GetTimeNow( double *time_returned ) { #ifdef WINDOWS static short first_time = TRUE; LARGE_INTEGER ticks = {0,0}; LARGE_INTEGER ticks_per_second = {0,0}; static double ticks_per_microsecond; if ( first_time == TRUE ) { QueryPerformanceFrequency (&ticks_per_second); ticks_per_microsecond = (float) ticks_per_second.LowPart / 1E6; first_time = FALSE; } QueryPerformanceCounter (&ticks); *time_returned = ticks.LowPart/ticks_per_microsecond; *time_returned += ldexp(ticks.HighPart,32)/ticks_per_microsecond; *time_returned /= 1E6; #endif #ifdef LINUX struct timeval tp; gettimeofday (&tp, NULL); *time_returned = tp.tv_usec; *time_returned /= 1E6; *time_returned += tp.tv_sec; #endif } ※ 編輯: autumned 來自: 61.231.96.222 (12/20 23:11)

12/20 23:12, , 7F
gcc有沒有跟你講說想編這種code要加個參數什麼的啊?
12/20 23:12, 7F

12/20 23:15, , 8F
QQ 沒用過gcc... 那我去找找看怎麼裝好咯QQ
12/20 23:15, 8F

12/20 23:18, , 9F
咦 XD我以為是gcc 那你是在哪個compiler編的?
12/20 23:18, 9F

12/20 23:19, , 10F
dev
12/20 23:19, 10F

12/20 23:42, , 11F
試試vc++也許可以編譯成功
12/20 23:42, 11F

12/20 23:44, , 12F
http://www.funp.net/index.php 直接放附件如何?
12/20 23:44, 12F

12/21 00:02, , 13F
mingw可以編 感謝各位的幫忙><
12/21 00:02, 13F

12/21 00:29, , 14F
前陣子還有maintain到類似的code XDD
12/21 00:29, 14F

12/21 01:16, , 15F
找1990左右的書都是這種寫法
12/21 01:16, 15F

12/21 11:54, , 16F
還不快跪下來膜拜!
12/21 11:54, 16F

12/21 15:17, , 17F
化石 code!
12/21 15:17, 17F

12/21 23:58, , 18F
這種寫法流行時我還沒出生!
12/21 23:58, 18F

12/22 01:33, , 19F
同樓上! 推
12/22 01:33, 19F

12/22 07:44, , 20F
奇怪,這種寫法我還是常在一些經典的書裡看到耶 XD
12/22 07:44, 20F

12/22 09:25, , 21F
啊對了 那個IFDEF LINUX ... 那段
12/22 09:25, 21F

12/22 09:25, , 22F
gcc還是不能compile 耶 而且 struct timeval tp;
12/22 09:25, 22F

12/22 09:26, , 23F
這個STRUCT從來沒有被定義過@@
12/22 09:26, 23F

12/22 09:26, , 24F
難道是真的是LINUX才能跑這段嗎XD
12/22 09:26, 24F

12/22 10:21, , 25F
timeval 定義在 sys/time.h,這是 POSIX 標準
12/22 10:21, 25F

12/22 10:22, , 26F
windows 要在 cygwin 環境下才能用
12/22 10:22, 26F

12/22 10:57, , 27F
好的@_@
12/22 10:57, 27F
文章代碼(AID): #1EyA7vci (C_and_CPP)