[英]How can i get isalnum() to accept strings properly
我正在嘗試轉換一些旨在從命令行參數中刪除除“ _”以外的所有非數字字符的代碼,而不是代替命令行參數,我正在嘗試獲取代碼以接受來自常規字符串的輸入,我試圖將代碼轉換為接受字符串,但是我一直收到此錯誤
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];
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.