我正在学习C,我试图找出如何用char数组创建数据库。我知道atoi使用字符串,但我不明白字符串和字符数组声明之间的区别(我明白字符串也有'/ 0'字符)。atoi()不能与char数组一起工作
它在该行int w = LoggersID[j];
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
int main()
{
// Logger ID Database
char LoggersID[50][2];
strcpy(LoggersID[1], "A");
strcpy(LoggersID[2], "B");
strcpy(LoggersID[3], "C");
strcpy(LoggersID[4], "D");
strcpy(LoggersID[5], "E");
strcpy(LoggersID[6], "F");
strcpy(LoggersID[7], "G");
strcpy(LoggersID[8], "H");
strcpy(LoggersID[9], "I");
strcpy(LoggersID[10], "J");
strcpy(LoggersID[11], "K");
strcpy(LoggersID[12], "L");
strcpy(LoggersID[13], "M");
strcpy(LoggersID[14], "N");
strcpy(LoggersID[15], "O");
strcpy(LoggersID[16], "P");
strcpy(LoggersID[17], "Q");
strcpy(LoggersID[18], "R");
strcpy(LoggersID[19], "S");
strcpy(LoggersID[10], "T");
strcpy(LoggersID[21], "1");
strcpy(LoggersID[22], "2");
strcpy(LoggersID[23], "3");
strcpy(LoggersID[24], "4");
strcpy(LoggersID[25], "5");
strcpy(LoggersID[26], "6");
strcpy(LoggersID[27], "7");
strcpy(LoggersID[28], "8");
strcpy(LoggersID[29], "9");
strcpy(LoggersID[30], "10");
printf("Lets start!\n");
for (int i = 65; i < 86; i++)
{
for (int j = 1; j < 31; j++)
{
int w = atoi(LoggersID[j]);
if (w == i)
{
printf("\nYou matched %s with %d", LoggersID[j], i);
}
}
}
for (int i = 1; i < 11; i++)
{
for (int j = 1; j < 31; j++)
{
int w = LoggersID[j];
if (w == i)
{
printf("\nYou matched %s with %d", LoggersID[j], i);
}
}
}
printf("\nProgram finished!");
getchar();
return 0;
}
编译 [Warning] initialization makes integer from pointer without a cast
时产生的警告当我运行它,我得到
Lets start!
Program finished!
,而不是匹配!
'的strcpy(LoggersID [30], “10”);'可能不是空终止 – byxor
'int w = LoggersID [j];' - >'int w = atoi(LoggersID [j]);'? – BLUEPIXY
'int w = atoi(LoggersID [j])'试图将字符串转换为数字,你似乎想要的是'int w = LoggersID [j] [0]'来比较ascii码 – Ctx