首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C与getline()斗争-不兼容的整数到指针转换

C与getline()斗争-不兼容的整数到指针转换
EN

Stack Overflow用户
提问于 2022-08-06 10:56:15
回答 1查看 51关注 0票数 1

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

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

以下是我收到的警告:

代码语言:javascript
复制
$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.

下面是我的完整代码(省略了用于紧凑/可读性的重复数组):

代码语言:javascript
复制
/*****************************************/
/*     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]);
        */

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-06 11:12:35

你在滥用getline()。您将传递一个char值作为getline()的第一个参数,而不是所需的char **指针到指针到char

根据POSIX getline() documentation (特别注意描述部分的粗体第二段):

概要

#包括 ssize_t getdelim(char **限制lineptr,size_t * ssize_t n,int分隔符,文件*限制流);ssize_t getline(char **限制lineptr,size_t *限制n,文件*限制流);

描述

getdelim()函数将从流中读取,直到遇到与分隔符字符匹配的字符为止。分隔符参数是int,应用程序应确保其值是一个字符,该字符可表示为终止读取进程的同等价值的unsigned char。如果分隔符参数有任何其他值,则行为未定义。

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

如果*lineptrnull指针,或者*lineptr指向的对象大小不足,则对象的分配应像由malloc()分配一样,或者对象应重新分配,就像由realloc()重新分配一样,使对象足够大以容纳要写入的字符,包括终止NUL,而*n应设置为新的大小。如果分配了对象,或者重新分配操作移动了对象,则将更新*lineptr以指向新对象或新位置。读取的字符(包括任何分隔符)应存储在对象中,并在遇到分隔符或文件末尾时添加终止NUL

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

正确使用getline()看起来更像这样:

代码语言:javascript
复制
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 变量E 272的地址,而不是调用sizeof()而不是地址的常量结果。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73259141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档