我有一个 char 文本数组,想将字母数字小写值存储在指针数组中。即 mystr 应该指向“50sometexthere”的 char[]
char[] myline = " 50 Some Text Here ";
char *mystr = (char *)malloc(128 * sizeof(char));
char *tmp = myline;
while (*tmp != '\0'){
if (isalnum(*tmp))
strcat(mystr,(char*) tolower(*tmp));
tmp++;
}
我做错了什么?
最佳答案
char *myline = " 50 Some Text Here ";
char *mystr = (char *)malloc(128); //sizeof char is always 1
char *tmp = myline;
char *tmpdest = mystr;
while (*tmp != '\0'){
if (isalnum(*tmp))
*tmpdest++ = tolower(*tmp); //this line is changed!
tmp++;
}
*tmpdest = '\0';
HTH
关于在 C 中复制字母数字字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4009155/