指针(char*)与整数错误的比较

Comparison between pointer (char*) and integer error

所以基本上用户在变量中输入他们访问了多少个国家,然后我将其作为数组的大小。

之后,我使用 for 循环列出所有访问过的国家。但我想让我的代码更聪明一点,并把 and 放在最后一个国家的句子末尾。

例如3个国家:

You visited Japan, Korea and Canada.
                 ^       ^^^
#include <stdio.h>
#include <cs50.h>


int main(void)
{
    int number_of_p = get_int("How many countries did you visit?\n");
    string countries[number_of_p];

    for (int x = 0; x < number_of_p; x++)
    {
        countries[x] = get_string("What countries did you visit?\n");
    }
    printf("You visited %i countries, including: ", number_of_p);
    for (int x = 0; x < number_of_p; x++)
    {

        printf("%s, ", countries[x]);
        /* looks for last element in arrays, inserts 'and' 
        to make it look grammatically correct. */

        if (countries[x] == number_of_p - 1 ) // ERROR HERE
        {
            printf(" and ");
        }

    }
    printf(".\n");

}

我正在比较指针 (char*) 和整数错误。

char* 是什么意思?

如何访问数组中的最后一个元素?

if语句中的条件

    if (countries[x] == number_of_p - 1 )

没有意义。左操作数 countries[x] 的类型为 char * 而右操作数的类型为 int.

那是类型说明符 string 是类型的别名 char * 和这个数组声明

string countries[number_of_p];

相同
char * countries[number_of_p];

所以你有一个指向字符串的指针数组。

C 中类型 char * 的别名 string 定义如下

typedef char * string;

循环看起来像

for (int x = 0; x < number_of_p; x++)
{
    if ( x != 0 )
    {
        putchar( ',' );
        putchar( ' ' );

        if ( x ==  number_of_p - 1 )
        {
            printf( "%s ", "and " );
        }
    }
    printf("%s", countries[x]);
}

这是一个演示程序

#include <stdio.h>

int main(void) 
{
    enum { number_of_p = 3 };
    char *countries[number_of_p] =
    {
        "Japan", "Korea", "Canada"
    };
    
    printf( "You visited %i countries, including: ", number_of_p );
    
    for (int x = 0; x < number_of_p; x++)
    {
        if ( x != 0 )
        {
            putchar( ',' );
            putchar( ' ' );

            if ( x ==  number_of_p - 1 )
            {
                printf( "%s ", "and" );
            }
        }
        printf("%s", countries[x]);
    }
    
    return 0;
}

它的输出是

You visited 3 countries, including: Japan, Korea, and Canada

您可以使用一个 printf 来完成此类操作,使用预定义的字符串作为分隔符。使用单个 printf 而不是特殊的外壳通常更容易维护列表的尾部。许多人发现三元运算符令人困惑;这是两种不同的方法,首先使用开关,然后使用三元运算符:

#include<stdio.h>

int main(void)
{
        char *countries[] = { "foo", "bar", "baz" };
        int number_of_p = sizeof countries / sizeof *countries;
        /* First loop, using a switch */
        for( int x = 0; x < number_of_p; x++ ) {
                char *div = ", ";
                switch( x ){
                case sizeof countries / sizeof *countries - 2: div = ", and "; break;
                case sizeof countries / sizeof *countries - 1: div = ".\n"; break;
                }
                printf("%s%s", countries[x], div);
        }
        /* Same loop as above, with if/else instead of the switch */
        for( int x = 0; x < number_of_p; x++ ) {
                char *div = ", ";
                if( x == number_of_p - 2 ) {
                        div = ", and ";
                } else if( x == number_of_p - 1 ) {
                        div = ".\n";
                }
                printf("%s%s", countries[x], div);
        }
        /* Same loop as above, written less verbosely */
        for( int x = 0; x < number_of_p; x++ ) {
                printf("%s%s", countries[x], x == number_of_p - 2 ? ", and " :
                        x == number_of_p - 1 ? ".\n" : ", ");
        }
}

countries[x] 是一个 string(在 CS50 中是 char* 的类型定义),number_of_p 是一个 int,你无法比较它们,它们是不同的类型,您可能想比较索引 x,这是对您的代码的可能(且快速)修复,包括标点符号可能如下所示:

Live demo

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    int number_of_p = get_int("How many countries did you visit?\n");
    string countries[number_of_p];

    for (int x = 0; x < number_of_p; x++)
    {
        countries[x] = get_string("What countries did you visit?\n");
    }
    printf("You visited %i countries, including: ", number_of_p);
    for (int x = 0; x < number_of_p; x++)
    {

        printf("%s", countries[x]);
        if(x < number_of_p - 2){
            printf(", ");
        }
   
        if (x == number_of_p - 2)
        {
            printf(" and ");
        }  
    }
    printf(".\n");
}

输入:

3
Japan
Korea
Canada

输出:

You visited 3 countries, including: Japan, Korea and Canada.