以下是一个合理的解决方案(即没有任何陷阱 表示或类型转换或某些定义的行为问题)? char s [SIZE]; unsigned char * t =(unsigned char *)s; int c; size_t i = 0; while((c = getchar())!= EOF&& c!=''\ n''&& i< SIZE - 1 ){ t [i] = c; / * ??? * / i ++; } s [i] =''\ 0''; - Enrico`Trippo''Porreca I agree, but AFAIK the implementor is allowed to be idiot...Am I right? Is the following a plausible solution (i.e. without any traprepresentation or type conversion or something-defined behaviour problem)? char s[SIZE];unsigned char *t = (unsigned char *) s;int c;size_t i = 0; while ((c = getchar()) != EOF && c != ''\n'' && i < SIZE - 1) {t[i] = c; /* ??? */i++;} s[i] = ''\0''; --Enrico `Trippo'' Porreca " Enrico`Trippo''Porreca" ; < TR **** @ lombardiacom.it>写道:"Enrico `Trippo'' Porreca" <tr****@lombardiacom.it> wrote:我同意,但AFAIK的实施者被允许是白痴... 我是对的吗? 是的,但请相信我,任何犯了char< - > int转换 的人都会破坏大部分被视为 是完全可移植的。因此他们的实施将不会出售。 考虑< ctype.h>函数,要求输入是一个int,其值在unsigned char范围内。那是 为什么我们建议人们像这样投射到unsigned char: char * p,s [] =" hello"; for(p = s; * p; p ++) * p = toupper((unsigned char)* p); 现在如果* p的值为负数,现在转换为无符号 char时,它是正数且超出signed char范围。所以理论上这个理论上可以超出int的范围,如果int和signed char具有相同的范围。因此你有相同的情况在 反向 - 无符号char到int转换不保证在范围内是 。 以下是a合理的解决方案(即没有任何陷阱表示或类型转换或某种定义的行为问题)? char s [SIZE]; unsigned char * t = (unsigned char *)s; int c; size_t i = 0; while((c = getchar())!= EOF&& c!= ''\\ n'&& i< SIZE - 1){ t [i] = c; / * ??? * / 赋值本身是安全的,但是因为它将任意的 表示放入数组的元素中,这是char 对象并且可能签名,它可能会生成陷阱 表示。那就是如果签名的char可以有陷阱 表示。我不太确定。 i ++; } s [i] =''\ 0''; I agree, but AFAIK the implementor is allowed to be idiot... Am I right?Yes, but trust me, anyone who fouled up the char<->int conversionwould break a large proportion of existing code that is consideredto be completely portable. Therefore their implementation wouldnot sell. Consider the <ctype.h> functions, which require that the input isan int whose value is within the range of unsigned char. That iswhy we suggest that people cast to unsigned char like this:char *p, s[] = "hello";for(p = s; *p; p++)*p = toupper((unsigned char)*p);Now if the value of *p was negative, now when converted to unsignedchar it is positive and outside the range of signed char. So thiscould theoretically be outside the range of int, if int and signedchar have the same range. Therefore you have the same situation inreverse - unsigned char to int conversion is not guaranteed to bewithin range. Is the following a plausible solution (i.e. without any trap representation or type conversion or something-defined behaviour problem)? char s[SIZE]; unsigned char *t = (unsigned char *) s; int c; size_t i = 0; while ((c = getchar()) != EOF && c != ''\n'' && i < SIZE - 1) { t[i] = c; /* ??? */The assignment itself is safe, but since it places an arbitraryrepresentation into the elements of the array s, which are charobjects and possibly signed, it might generate a traprepresentation. That is if signed char can have traprepresentations. I''m not completely sure. i++; } s[i] = ''\0''; - 西蒙。 --Simon. 这篇关于行输入和实现定义的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
11-01 20:03