I want the int value of the second char in 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:
The last line is an assignment. The right-hand-side (
*(str + 1)) haschartype. The left-hand-side is anint. 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