我正在尝试转换一些代码,这些代码旨在从命令行参数中删除除“_”之外的所有非数字字符,除了我试图让代码接受来自常规字符串,我尝试将代码转换为接受字符串,但我一直收到此错误
words.c:9: warning: assignment makes pointer from integer without a cast
我对自己做错了什么感到困惑,所以我非常感谢能为我解决这个问题提供的任何帮助,谢谢!
这里还有接受命令行参数的原始代码
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char ** argv) {
int i;
char *p;
if (argc > 1) {
for (p = argv[1]; *p != '\0'; p++) {
if (islower(*p) || isdigit(*p) || *p == '_') {
putchar (*p);
}
}
putchar ('\n');
}
return 0;
}
这是我的“版本”
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void) {
int i;
char *p;
char stg[] = "hello";
// if (argc > 1) {
for (p = stg[1]; *p != '\0'; p++) {
if (isalnum(*p) || *p == '_') {
putchar (*p);
}
}
putchar ('\n');
return 0;
}
最佳答案
在你的代码中 p
是一个指针。将 p = stg[1];
更改为 p = &stg[1];
。
关于c - 我怎样才能让 isalnum() 正确地接受字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10559151/