[問題] 這段三元運算子的意思是?
開發平台(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
推
11/28 01:36, , 1F
11/28 01:36, 1F
→
11/28 01:38, , 2F
11/28 01:38, 2F
→
11/28 02:48, , 3F
11/28 02:48, 3F
→
11/28 02:49, , 4F
11/28 02:49, 4F
→
11/28 02:50, , 5F
11/28 02:50, 5F
→
11/28 06:03, , 6F
11/28 06:03, 6F
→
11/28 06:03, , 7F
11/28 06:03, 7F
→
11/28 06:04, , 8F
11/28 06:04, 8F
→
11/28 18:21, , 9F
11/28 18:21, 9F
推
11/29 21:57, , 10F
11/29 21:57, 10F
→
11/29 21:57, , 11F
11/29 21:57, 11F
→
11/30 22:27, , 12F
11/30 22:27, 12F
→
11/30 22:30, , 13F
11/30 22:30, 13F
→
12/03 00:29, , 14F
12/03 00:29, 14F
→
12/03 01:14, , 15F
12/03 01:14, 15F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章