简体   繁体   中英

How can i get isalnum() to accept strings properly

I am trying to convert some code that is meant to remove all non-numeric characters except for "_" from a command line argument, except instead of a command line argument I am trying to get the code to accept input from a regular string, I've tried to convert the code to accept strings, but i keep getting this error

words.c:9: warning: assignment makes pointer from integer without a cast

I am confused as to what I am doing wrong so I would really appreciate any help that I can get with this problem, thanks!

Also here is the original code that accepts command line arguments

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

and here is my "version"

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

In your code p is a pointer. Change p = stg[1]; to p = &stg[1]; .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM