C 挣扎于 getline() - 不兼容的整数到指针的转换



我需要我的程序收集随机用户输入,以便将字符数返回为奇数或偶数(return ( strlen ( &raw_scrap ) % 2 ) ;)

我无法从全局声明中char raw_scrapgetline()函数正常工作。

以下是我收到的警告:

$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.

这是我的完整代码(省略重复数组以实现紧凑性/可读性):

/*****************************************/
/*     Geomant : a basic implementation  */
/*  of the geomanteia intended as a first*/
/*  C training project                   */
/*  copyright xc2xa9 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 **指针到指针到char

根据 POSIXgetline()文档(特别注意描述部分的加粗第二段):

概要

#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()函数应从流中读取,直到遇到与分隔符字符匹配的字符。分隔符 参数是一个int,应用程序应确保其值 是可表示为具有同等价值的unsigned char的字符 终止读取过程。如果分隔符参数具有任何其他 值,则行为未定义。

应用程序应确保*lineptr是可以传递给free()函数的有效参数。如果*n不为零, 申请应确保*lineptr任一指向 大小至少为*n字节的对象,或者是null指针。

如果*lineptrnull指针,或者对象指向*lineptr大小不足,则应像分配对象一样分配 通过malloc()或对象应重新分配,就好像通过realloc()一样, 分别使对象足够大以容纳 要写入的字符,包括终止NUL,以及*n应设置为新的大小。如果对象已分配,或者如果 重新分配操作移动了对象,*lineptr应 更新为指向新对象或新位置。人物介绍 读取,包括任何分隔符,应存储在对象中,并且NUL当分隔符或文件结尾为 遇到。

getline()函数应等效于分隔符等于<newline>字符的getdelim()函数。

正确使用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()的常量结果,这不是地址。

另请注意,这些参数中包含的值必须符合 POSIX 标准中指出的限制。

相关内容

  • 没有找到相关文章