【问题标题】:Indexing from a list of strings in C?从C中的字符串列表索引?
【发布时间】:2021-10-29 09:16:56
【问题描述】:

明显的新手问题,我正在尝试写一些你放在 int 中的东西,它会给你一个月。这是一个函数python版本:

months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
def get_month(mon):
    return months[mon]

在 C 中,我已经这样做了:


#include <stdio.h>
char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};


int main()
{
char get_month(int m)
{
    return *months[m];
}
        
for (int i = 0; i < 12; i++)
{
    char    this_month = get_month(i);
    printf ("Month %d is %c\n", i, this_month);
}

return 0;
}

我得到这个输出

Month 1 is J
Month 2 is F
Month 3 is M
Month 4 is A
Month 5 is M
Month 6 is J
Month 7 is J
Month 8 is A
Month 9 is S
Month 10 is O
Month 11 is N
Month 12 is D

我认为我必须以某种方式考虑 *months[12] 中字符串的长度(或者它们在技术上是字符吗?),但我不知道如何。如果我将其更改为 *months[12][3] 我会得到

warning: returning ‘char *’ from a function with return type ‘char’ makes integer from pointer without a cast [-Wint-conversion]

另外,如果它们的长度不一样(即月份被完全写出来)怎么办?

【问题讨论】:

  • 所以你想得到整个月?
  • 考虑一下:get_month 返回一个char,但您希望它返回一个字符串,即指向字符数组char * 的指针,那么解决方案应该很明显。

标签: arrays c char c-strings function-definition


【解决方案1】:
  1. 功能:
char get_month(int m)  // this declares function which returns a single char
{
    return *months[m]; // months is array of pointers. months[m] gives pointer to string. *months[m] dereferences that pointer giving the first character of the month name.
}

必须是:

char *get_month(int m)  // this declares function which returns pointer to the char array.
{
    return months[m]; // months is array of pointers. months[m] gives pointer to the char array containing the month name
}
  1. 不要在其他函数中定义函数。

将您的函数定义移到main 函数之外。

  1. 您的变量必须是指向 char 的指针类型,因为 C 中的字符串是 chars 的数组:char *this_month = get_month(i);

  2. 使用正确的 printf 格式: printf ("Month %d is %s\n", i, this_month);

  3. 使用正确的main 定义。如果它不带参数,它必须是int main(void)

  4. 正确格式化您的代码

#include <stdio.h>
char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

char *get_month(int m)
{
    return months[m];
}

int main()
{
    for (int i = 0; i < 12; i++)
    {
        char    *this_month = get_month(i);
        printf ("Month %d is %s\n", i, this_month);
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    在另一个函数中定义一个函数不是标准的 C 特性。

    你需要把函数get_month的定义移到main上面。

    数组monhts 的元素具有char * 类型。

    char *months[12] = { /*... */ };
    

    所以返回数组元素的函数get_month必须与数组元素的类型具有相同的返回类型。

    char * get_month(int m)
    {
        return months[m];
    }
    

    所以在 main 的 for 循环中你需要写

    char   *this_month = get_month(i);
    printf ("Month %d is %s\n", i, this_month);
    

    至于这个表达式*months[m] 然后它产生指针months[m] 指向的字符串的第一个字符,因为你有一个数组pf 指针。

    【讨论】:

      【解决方案3】:

      在您的方法中,返回类型是 char 而不是 char*,因此您只能返回单个字符而不是字符串。

      char get_month(int m)
      {
          return *months[m];
      }
      

      下一个问题是你取消引用从数组中查找的值 因此,取而代之的是指向字符串的指针,您会在该字符串的起始地址(即第一个字母)处获得值。

      试试这个:

      char* get_month(int m)
      {
          return months[m];
      }
      

      【讨论】:

      • 也是printf 格式说明符。 %c -> %s.
      • 还有char this_month =
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-22
      • 2013-07-05
      • 2019-04-29
      • 2021-08-11
      • 1970-01-01
      相关资源
      最近更新 更多