[英]C struggling with getline() - incompatible integer to pointer conversion
我需要我的程序收集隨機用戶輸入,以便將字符數返回為奇數或偶數( return ( strlen ( &raw_scrap ) % 2 );
)
我無法讓getline()
function 從char raw_scrap
全局聲明中正常工作。
以下是我收到的警告:
$clang geomant.c -o geomant.bin
geomant.c:61:12: warning: incompatible integer to pointer conversion passing 'char' to parameter of type 'char **' [-Wint-conversion]
getline ( raw_scrap, sizeof(&raw_scrap), stdin ) ;
^~~~~~~~~
/usr/include/stdio.h:239:36: note: passing argument to parameter here
ssize_t getline(char ** __restrict, size_t * __restrict,
^
geomant.c:61:23: warning: incompatible integer to pointer conversion passing 'unsigned long' to parameter of type 'size_t *' (aka 'unsigned long *') [-Wint-conversion]
getline ( raw_scrap, sizeof(&raw_scrap), stdin ) ;
^~~~~~~~~~~~~~~~~~
/usr/include/stdio.h:239:57: note: passing argument to parameter here
ssize_t getline(char ** __restrict, size_t * __restrict,
^
2 warnings generated.
這是我的完整代碼(為了緊湊/可讀性,省略重復的 arrays):
/*****************************************/
/* Geomant : a basic implementation */
/* of the geomanteia intended as a first*/
/* C training project */
/* copyright \xc2\xa9 Sylvain Saboua */
/* <sylvain ! saboua (|) free ! fr> */
/*****************************************/
# include <stdlib.h>
# include <stdio.h>
# include <string.h>
# include <unistd.h>
char *body_parts[] = { "head", "neck", "body", "legs" } ;
char *mother_figures[] = { "1st", "2nd", "3rd", "4th" } ;
int n_figure ;
int n_bodypart ;
char raw_scrap ;
char *figures[15][4] = {
// mothers
{"* *", "* *", "* *", "* *"},
{"* *", "* *", "* *", "* *"},
{"* *", "* *", "* *", "* *"},
{"* *", "* *", "* *", "* *"},
// daughters
{"* *", "* *", "* *", "* *"},
{"* *", "* *", "* *", "* *"},
{"* *", "* *", "* *", "* *"},
{"* *", "* *", "* *", "* *"},
// nieces
{"* *", "* *", "* *", "* *"},
{"* *", "* *", "* *", "* *"},
{"* *", "* *", "* *", "* *"},
{"* *", "* *", "* *", "* *"},
// witnesses & judge
{"* *", "* *", "* *", "* *"},
{"* *", "* *", "* *", "* *"},
{"* *", "* *", "* *", "* *"}
} ;
/*
"mantize" : input fuction
This function gathers a string of user input
and returns a 0 or 1 for the evenness or
oddity of the number of characters. It is called
in a sequence by the next function to generate
the figures one by one.
*/
int mantize ( int n_bodypart, int n_figure ) {
printf ( "Hold or repeatedly press a key or keys to generate the %s of the %s mother
, then press Enter:\n",
body_parts[n_bodypart], mother_figures[n_figure] ) ;
getline ( raw_scrap, sizeof(&raw_scrap), stdin ) ;
return ( strlen ( &raw_scrap ) % 2 ) ;
}
/*
"generate" fuction:
This function takes the cast result (being 0 or 1,
odd or even) and use it to generate
all 15 figures, from the mothers down to the
daughters, nieces, witnesses and judge.
*/
int generate(){
// generating the mothers
for ( n_figure = 0 ; n_figure < 4 ; n_figure++ ) {
for ( n_bodypart = 0 ; n_bodypart < 4 ; n_bodypart++ ) {
figures[n_figure][n_bodypart] =
(0 == mantize(n_bodypart, n_figure)) ? "* *" : "*" ;
}
}
//generating the four daughters from the four mothers
figures[4][0] = figures[0][0] ;
figures[4][1] = figures[1][0] ;
figures[4][2] = figures[2][0] ;
figures[4][3] = figures[3][0] ;
figures[5][0] = figures[0][1] ;
figures[5][1] = figures[1][1] ;
figures[5][2] = figures[2][1] ;
figures[5][3] = figures[3][1] ;
figures[6][0] = figures[0][2] ;
figures[6][1] = figures[1][2] ;
figures[6][2] = figures[2][2] ;
figures[6][3] = figures[3][2] ;
figures[7][0] = figures[0][3] ;
figures[7][1] = figures[1][3] ;
figures[7][2] = figures[2][3] ;
figures[7][3] = figures[3][3] ;
// generating the nieces
figures[8][0] = ( figures[0][0] == figures[1][0] ) ? "* *" : " * " ;
figures[8][1] = ( figures[0][1] == figures[1][1] ) ? "* *" : " * " ;
figures[8][2] = ( figures[0][2] == figures[1][2] ) ? "* *" : " * " ;
figures[8][3] = ( figures[0][3] == figures[1][3] ) ? "* *" : " * " ;
figures[9][0] = ( figures[2][0] == figures[3][0] ) ? "* *" : " * " ;
figures[9][1] = ( figures[2][1] == figures[3][1] ) ? "* *" : " * " ;
figures[9][2] = ( figures[2][2] == figures[3][2] ) ? "* *" : " * " ;
figures[9][3] = ( figures[2][3] == figures[3][3] ) ? "* *" : " * " ;
figures[10][0] = ( figures[4][0] == figures[5][0] ) ? "* *" : " * " ;
figures[10][1] = ( figures[4][1] == figures[5][1] ) ? "* *" : " * " ;
figures[10][2] = ( figures[4][2] == figures[5][2] ) ? "* *" : " * " ;
figures[10][3] = ( figures[4][3] == figures[5][3] ) ? "* *" : " * " ;
figures[11][0] = ( figures[6][0] == figures[7][0] ) ? "* *" : " * " ;
figures[11][1] = ( figures[6][1] == figures[7][1] ) ? "* *" : " * " ;
figures[11][2] = ( figures[6][2] == figures[7][2] ) ? "* *" : " * " ;
figures[11][3] = ( figures[6][3] == figures[7][3] ) ? "* *" : " * " ;
// generating the witnesses
figures[12][0] = ( figures[8][0] == figures[9][0] ) ? "* *" : " * " ;
figures[12][1] = ( figures[8][1] == figures[9][1] ) ? "* *" : " * " ;
figures[12][2] = ( figures[8][2] == figures[9][2] ) ? "* *" : " * " ;
figures[12][3] = ( figures[8][3] == figures[9][3] ) ? "* *" : " * " ;
figures[13][0] = ( figures[10][0] == figures[11][0] ) ? "* *" : " * " ;
figures[13][1] = ( figures[10][1] == figures[11][1] ) ? "* *" : " * " ;
figures[13][2] = ( figures[10][2] == figures[11][2] ) ? "* *" : " * " ;
figures[13][3] = ( figures[10][3] == figures[11][3] ) ? "* *" : " * " ;
//generating the judge
figures[14][0] = ( figures[12][0] == figures[13][0] ) ? "* *" : " * " ;
figures[14][1] = ( figures[12][1] == figures[13][1] ) ? "* *" : " * " ;
figures[14][2] = ( figures[12][2] == figures[13][2] ) ? "* *" : " * " ;
figures[14][3] = ( figures[12][3] == figures[13][3] ) ? "* *" : " * " ;
return *figures[14][3] ;
}
/*
"display" function:
This function will display onscreen the final
geomantic Shield for the given divination.
The "-" & "|" characters are used for separation
while the dots are displayed using "*"
*/
int main(){
generate();
//printf(*figures);
/*
printf(" --- ")
for i in .*
return(*raw_scrap[3][3]);
*/
}
您在濫用getline()
。 您將char
值作為getline()
的第一個參數傳遞,而不是所需的char **
pointer-to-pointer-to- char
。
根據POSIX getline()
文檔(特別注意描述部分的粗體第二段):
概要
#include <stdio.h> ssize_t getdelim(char **restrict lineptr, size_t *restrict n, int delimiter, FILE *restrict stream); ssize_t getline(char **restrict lineptr, size_t *restrict n, FILE *restrict stream);
描述
getdelim()
function 應從 stream 讀取,直到遇到與分隔符匹配的字符。 delimiter 參數是一個int
,應用程序應確保其值是一個可表示為終止讀取過程的等值unsigned char
的字符。 如果 delimiter 參數具有任何其他值,則行為未定義。應用程序應確保
*lineptr
是可以傳遞給free()
function 的有效參數。 如果*n
不為零,則應用程序應確保*lineptr
指向大小至少為*n
字節的 object,或者是null
指針。If
*lineptr
is anull
pointer or if the object pointed to by*lineptr
is of insufficient size, an object shall be allocated as if bymalloc()
or the object shall be reallocated as if byrealloc()
, respectively, such that the object 足夠大以容納要寫入其中的字符,包括終止NUL
,並且*n
應設置為新大小。 如果分配了 object,或者如果重新分配操作移動了 object,則*lineptr
應更新為指向新的 object 或新位置。 讀取的字符(包括任何分隔符)應存儲在 object 中,並在遇到分隔符或文件結尾時添加終止NUL
。
getline()
function 應等效於getdelim()
function,分隔符字符等於<newline>
字符。
正確使用getline()
看起來更像這樣:
char *raw_scrap = NULL;
size_t raw_scrap_size = 0UL;
.
.
.
getline ( &raw_scrap, &raw_scrap_size, stdin ) ;
getline()
讀取整行輸入-您必須更改程序以使用行,而不是單個char
輸入。
請注意, raw_scrap
必須是指向char
的指針,並且該指針的地址必須傳遞給getline()
,第二個參數是實際size_t
變量的地址,而不是調用sizeof()
的常量結果,這不是一個地址。
另請注意,這些 arguments 中包含的值必須符合 POSIX 標准中注明的限制。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.