【问题标题】:Why <null> apears every time I call a different function?为什么每次调用不同的函数时都会出现 <null> ?
【发布时间】:2019-01-04 16:21:00
【问题描述】:

作为一个编程新手,我犯了很多错误,但我无法理解完美的指针。我写了一个代码,在函数 Add 中添加了有关食物时间表的数据。在使用函数 Modify 之后,我希望我的代码打印最后添加的数据.但这是问题所在,它每次都会打印出来。你知道如何解决吗?

int login ( char name[50] );
void autoPass( char username[50] );
char pass[50];
int n = -1; //global counter
char *table[100][4];
void Add( int n );
void Modify( int n );

int main()
{
    char selection[7];
    printf( "\n\nChoose between: Add,Modify,View,Search,Sort,Exit\nSelection: " );
    scanf ( "%s",&selection );

    while ( ( strcmp( selection,"Exit" )  == 0 ) == 0 )
    {
        if (strcmp( selection,"Add" ) == 0 )
        {
            Add( n );
            printf( "Press any character to continue or Exit to finish.\n" );
            scanf ( "%s",&selection );
            printf( "check1" );
        }else if ( strcmp( selection,"Modify" ) == 0 )
        {
            Modify( n );
            printf( "Press any character to continue or Exit to finish.\n" );
            scanf ( "%s", &selection );
            printf( "check2" );
        }
    }
}



void Add (int n)
{
    int i,j;
    n++;
    for ( j = 0 ; j < 4 ; j++ )
    {
        if ( j == 0 )
        {
            printf ( "Enter a food:\n" );
            table[n][j] = (char*) malloc( 30 );
            scanf( "%s",table[n][j] );
        }else if (j == 1 )
        {
            printf("Enter calories:\n");
            table[n][j]=(char*) malloc(30);
            scanf("%s",table[n][j]);
        }else if ( j == 2 )
        {
            printf( "Enter the time you ate:\n" );
            table[n][j] = (char*) malloc( 30 );
            scanf ( "%s",table[n][j] );
        }else if ( j == 3 )
        {
            if (atof( table[n][j-1]) >= 5.00 && atof( table[n][j-1] ) <= 11.59 )
            {
                table[n][j] = "prwino";
            }else if( atof( table[n][j-1] ) >= 12.00 && atof( table[n][j-1] ) <= 19.59 )
            {
                table[n][j] = "mesimeriano";
            }else if ( atof( table[n][j-1] ) >= 20.00 && atof( table[n][j-1] ) <= 4.59 )
            {
                table[n][j] = "vradino";
            }
        }
    }
}

void Modify( int n )
{
    int i,j;
    for ( j=0 ; j < 4 ; j++ )
    {
       printf ( "%s",table[n][j]," " );
     }
     printf( "\n" );
 }

我希望我的 Modify 函数能够打印我之前使用 Add 函数添加的数据。而不是每次打印我时都这样。

【问题讨论】:

    标签: c arrays function pointers


    【解决方案1】:

    嗯,你的代码似乎有一些问题。

    你应该替换所有 scanf 调用:

    scanf ( "%s",&selection );
    

    并将它们更改为:

    scanf ( "%s",selection ); /// Drop the &
    

    您需要删除&amp;,因为%s 格式需要char* 类型的参数,但是当您使用&amp;selection 时,参数变为char (*)[7],而不是您声明的char*

    你也应该总是检查scanf的返回值:

    if ( scanf ( "%6s",selection ) != 1 ) ///notice 6 there?
    {
        printf("Error, scanf()\n" );///Is there to read only 6...
        exit( EXIT_FAILURE );
    }
    

    另一个问题是在void Add ( int n ) 内部,你增加n

    n++;
    

    但您也将n 声明为全局: int n = -1; //global counter

    您希望增加哪个n

    modify() 函数中,你的参数太多了:

    printf ( "%s",table[n][j]," " );
    

    删除最后一个" "

    printf ( "%s",table[n][j] );

    作为建议,您可以使用strcasecmp 而不是strcmp。 您需要为此添加strings.h

    【讨论】:

    • 非常感谢!你回答解决我的问题!再次感谢您!
    • 问题..为什么我应该在选择时去掉''&''?我不明白为什么
    猜你喜欢
    • 2020-05-10
    • 2020-10-14
    • 2016-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多