本文介绍了功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 C 中的 toupper() 函数是如何工作的.我正在下面的代码中尝试它,但我肯定做错了什么.代码可以编译,但传递给 toupper() 的参数没有大写...

I am wondering how the toupper() function in C works. I am trying it out in the code below but I'm definitely doing something wrong. The code compiles, but the arguments passed into toupper() are not being capitalized...

char **copyArgs(int argc, char **argv) {
    char **a = malloc(sizeof(char *) * (argc));

    int i;
    for(i = 0; i < argc; i++) {
        int size = strlen(argv[i]);
        a[i] = malloc(sizeof(char) * (size + 1));
        strcpy(a[i], argv[i]);
        a[i] = toupper(a[i]);
    }
    return a;
}

如果我用一二"测试这个结果是一二",而不是一二".任何建议表示赞赏.

If I test this with "one two" it results in "one two", not "ONE TWO". Any advice is appreciated.

推荐答案

toupper 将单个字母转换为大写.在您的情况下,由于 C 在隐式转换中的宽恕,您传递的是指向它的指针而不是 char ,因此很明显它无法正常工作.您可能会收到没有强制转换的隐式整数转换指针"警告:这是一个强烈的信号,表明您做错了什么.

toupper converts a single letter to uppercase. In your case, you are passing a pointer to it instead of a char thanks to C's forgiveness in implicit conversions, so it's obvious that it doesn't work correctly. Probably you are getting an "implicit pointer to integer conversion without a cast" warning: this is a strong sign that you are doing something very wrong.

整个事情不会仅仅因为在您的平台上 int 与指针一样大(或者,至少对于您正在使用的那些指针足够大)而爆炸;toupper 尝试将 int 解释为一个字符,发现它是非字母的,并未经修改地返回它.这纯粹是运气,在其他平台上,您的程序可能会崩溃,因为指向 int 转换的指针被截断,并且因为 toupper 之外的整数上的行为unsigned char 范围(加上 EOF)未定义.

The whole thing doesn't blow up just because on your platform int is as big as a pointer (or, at least, big enough for those pointers you are using); toupper tries to interpret that int as a character, finds out that it's non-alphabetic and returns it unmodified. That's sheer luck, on other platforms your program would probably crash, because of truncation in the pointer to int conversion, and because the behavior of toupper on integers outside the unsigned char range (plus EOF) is undefined.

要将整个字符串转换为大写,您必须遍历其所有字符并对每个字符调用 toupper.您可以轻松编写一个函数来执行此操作:

To convert a whole string to uppercase, you have to iterate over all its chars and call toupper on each of them. You can easily write a function that does this:

void strtoupper(char *str)
{
    while(toupper((unsigned char)*str++))
        ;
}

注意 unsigned char 强制转换 - 所有处理字符分类和转换的 C 函数需要一个 int 或者 EOF(保持不变)或者是 unsigned char 的值.原因既悲伤又复杂,我已经在在另一个答案中详细说明了.

Notice the unsigned char cast - all C functions dealing with character categorization and conversion require an int that is either EOF (which is left intact) or is the value of an unsigned char. The reason is sad and complex, and I already detailed it in another answer.

不过,值得注意的是,toupper 按设计不能可靠地使用多字节字符编码(例如 UTF-8),因此它在现代文本中没有真正的位置处理(一般来说,大多数 C 语言环境设施都是在另一个时代(糟糕的)设计的).

Still, it's worth noting that toupper by design cannot work reliably with multibyte character encodings (such as UTF-8), so it has no real place in modern text processing (as in general most of the C locale facilities, which were (badly) designed in another era).

这篇关于功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 22:17