开发者

How can this conversion work in C?

I want the int value of the second char in 开发者_JS百科a string; NOT the ASCII value, just whatever is stored in there.

int strLen = 0;  
char str[] = { 'b', 3, 'd', 'e', 'f' };   

strLen = strLen | *(str + 1);    

/* this prints 3; it's OK */
printf("strLen => %d\r\n", strLen);    

Now, this would get the value of the second char which is 3, but also the value of the 'b' which is 98, so I'd end up with something other than 3, which is what I want.

However, this works too and the result is still 3:

strLen = *(str + 1);

printf("strLen => %d\r\n", strLen);  

Can anyone explain why?


Taking a guess at the core mis-understanding here I want to concentrate on a reduced portion of the code here:

int strLen = 0;  
char str[] = { 'b', 3, 'd', 'e', 'f' }; 
strLen = *(str + 1);

The last line is an assignment. The right-hand-side (*(str + 1)) has char type. The left-hand-side is an int. What the compiler when assigning a small integer type to a large one is simple express the value of the small typed expression in the larger type.

So the assignment is equivalent to

strLen = 3;


Bit-wise or-ing something with zero give you the original result:

x | 0 == x

So given strlen = 0

strLen | *(str + 1) == *(str + 1)

Adding the bit-wire or does nothing, so both of your examples are exactly equivalent, so of course they produce the same result. It's not clear why you put the bit-wise or in there at all in the first case any way, it serves no purpose.

In any case any de-reference of str (including any addition of it), since it is a char* will always result in a char - that is one character. *(str) == 'b', *(str+1) == 3, *(str+2) == 'd', etc. If you want to get more than two characters out of the array then just get two characters:

printf("%d %d\r\n", *str, *(str+1)); 


This is a hard question to answer because it is difficult to work out what mental model you have of the situation.

I want the int value of the second char in a string; NOT the ASCII value, just whatever is stored in there.

The value stored in the second char in a string is accessed via str[1] or *(str + 1) or possibly *++str. The result is the value stored in the string. In your sample string, the value is 3; this character is also known as CONTROL-C. This is different from the value that would be stored there for the digit '3'; that would typically be stored as 51 (assuming a code page based on ISO 8859-1 or one of its close relatives that uses the same character code allocation as ASCII).

The strLen = 0; and then strLen = strLen | *(str + 1); achieve the same result as:

strLen = str[1];
strLen = *(str + 1);

In both cases, strLen now contains the value 3.

The printing statement then converts the integer value 3 into a string which contains:

strLen => 3

followed by a CR and an LF. (The CR is unnecessary, even on a Windows machine. However, that's a diversion from the issue at hand.)

These characters are then sent to the standard output. Note that the value at position 10 in the output (counting from zero) is the same as '3', typically 51, and distinct from the value 3 stored in the string. Your format requested this conversion. If you want to print the character, then you say so:

printf("strLen => %c\n", strLen);

This will print the CONTROL-C to the output; what that will look like depends on your terminal driver and terminal settings.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜