我怎样才能让isalnum()正确接受字符串

时间:2012-05-11 21:55:21

标签: c

我正在尝试转换一些代码,这些代码旨在从命令行参数中删除除“_”之外的所有非数字字符,除了代替命令行参数我试图让代码接受来自a的输入常规字符串,我试图将代码转换为接受字符串,但我一直收到此错误

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;
}

1 个答案:

答案 0 :(得分:3)

在您的代码p中是一个指针。将p = stg[1];更改为p = &stg[1];