為什麼這篇man 3 printf意思鄉民發文收入到精華區:因為在man 3 printf意思這個討論話題中,有許多相關的文章在討論,這篇最有參考價值!作者wtchen (沒有存在感的人)看板C_and_CPP標題[問題] 這段三元運算子的意思是?時間...
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
Linux + gcc5.2.1
額外使用到的函數庫(Library Used): (Ex: OpenGL, ...)
問題(Question):
看到一個面試題目:
typedef int* TYPE;
int f(TYPE x) ;
//Allocate an array of two integers;
//return 0 if the allocation succeeded, 1 otherwise.
//Return the allocated array in x
int f(TYPE x) {
int* ret = (int*) malloc(sizeof(int[2]));
return ret ? *x=ret, 0:1 ; //-> 這行不懂
}
TYPE 該怎麼定義?
我試過只有typedef int* TYPE 可以編譯過
可是我不懂最後一行
一般來說三元運算子不是
val = x ? y : z ;
等於
if(x == true) val = y;
else val = z;
可是函式這行我不懂...
餵入的資料(Input):
預期的正確結果(Expected Output):
錯誤結果(Wrong Output):
程式碼(Code):(請善用置底文網頁, 記得排版)
以下是我的測試程式:
#include <stdio.h>
#include <stdlib.h>
typedef int* TYPE;
int f(TYPE x) {
int* ret = (int*) malloc(sizeof(int[2]));
return ret ? *x=ret, 0:1 ;
}
int main(void) {
TYPE a;
printf("%d\n", f(a));
return 0;
}
補充說明(Supplement):
編譯結果(gcc Q_15.c -Wall):
Q_15.c: In function ‘f’:
Q_15.c:7:20: warning: assignment makes integer from pointer without a cast [-Wint-conversion]
return ret ? *x=ret, 0:1 ;
^
Q_15.c: In function ‘main’:
Q_15.c:12:5: warning: ‘a’ is used uninitialized in this function [-Wuninitialized]
printf("%d\n", f(a));
^
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 90.41.112.247
※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1448643722.A.577.html
※ 編輯: wtchen (90.41.112.247), 11/28/2015 01:11:09