从 C 中的字符串列表进行索引?
Indexing from a list of strings in C?
明显的新手问题,我正在尝试编写一些您放入 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
我想我必须以某种方式解释 * 个月 [12] 中字符串的长度(或者它们在技术上是字符?),但我不知道如何计算。如果我将其更改为 *months[12][3] 我得到
warning: returning ‘char *’ from a function with return type ‘char’ makes integer from pointer without a cast [-Wint-conversion]
此外,如果它们的长度不一样(即月份写得完整)怎么办?
在您的方法中,return 类型是 char 而不是 char*,因此您只能 return 单个字符而不是字符串。
char get_month(int m)
{
return *months[m];
}
下一个问题是取消引用从数组中查找的值
因此,取而代之的是指向字符串的指针,您在该字符串的起始地址处获得值,这是第一个字母。
试试这个:
char* get_month(int m)
{
return months[m];
}
在另一个函数中定义一个函数不是标准的 C 功能。
您需要将函数 get_month
的定义移动到 main 之上。
数组 monhts
的元素具有类型 char *
。
char *months[12] = { /*... */ };
所以 return 数组元素的函数 get_month
必须与数组元素的类型具有相同的 return 类型。
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 pointers.
- 函数:
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
}
- 不要在其他函数中定义函数。
将您的函数定义移到 main
函数之外。
您的变量必须是指向 char 的指针类型,因为 C 中的字符串是 char
s 的数组:char *this_month = get_month(i);
使用正确的 printf 格式:
printf ("Month %d is %s\n", i, this_month);
使用正确的 main
定义。如果它不带参数,它必须是 int main(void)
正确设置代码格式
#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;
}
明显的新手问题,我正在尝试编写一些您放入 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
我想我必须以某种方式解释 * 个月 [12] 中字符串的长度(或者它们在技术上是字符?),但我不知道如何计算。如果我将其更改为 *months[12][3] 我得到
warning: returning ‘char *’ from a function with return type ‘char’ makes integer from pointer without a cast [-Wint-conversion]
此外,如果它们的长度不一样(即月份写得完整)怎么办?
在您的方法中,return 类型是 char 而不是 char*,因此您只能 return 单个字符而不是字符串。
char get_month(int m)
{
return *months[m];
}
下一个问题是取消引用从数组中查找的值 因此,取而代之的是指向字符串的指针,您在该字符串的起始地址处获得值,这是第一个字母。
试试这个:
char* get_month(int m)
{
return months[m];
}
在另一个函数中定义一个函数不是标准的 C 功能。
您需要将函数 get_month
的定义移动到 main 之上。
数组 monhts
的元素具有类型 char *
。
char *months[12] = { /*... */ };
所以 return 数组元素的函数 get_month
必须与数组元素的类型具有相同的 return 类型。
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 pointers.
- 函数:
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
}
- 不要在其他函数中定义函数。
将您的函数定义移到 main
函数之外。
您的变量必须是指向 char 的指针类型,因为 C 中的字符串是
char
s 的数组:char *this_month = get_month(i);
使用正确的 printf 格式:
printf ("Month %d is %s\n", i, this_month);
使用正确的
main
定义。如果它不带参数,它必须是int main(void)
正确设置代码格式
#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;
}