By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,898 Members | 2,039 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,898 IT Pros & Developers. It's quick & easy.

Array Lookup [Was: Why is "Hello World" const char* ?]

P: n/a
Hello,

Thanks for all your response on my question about signed/unsigned chars.

The problem is this: I want to use the char array for a ArrayLookup, so
I need to convert char to unsigned int.

But this doesn't work!

If I try this:

#include <stdio.h>
int main() {
char c = -1; // 129 unsigned and -1 signed right?
// convert it to a value between 0 - 255 for array Lookup
unsigned int arrayIndex = (unsigned int) c;
if (arrayIndex < 0)
printf("ArrayIndex still negative after conversion to unsigned int
%d\n",arrayIndex);
else
printf("Unsigned representation is OK for ArrayIndex=%d\n",arrayIndex);
}

The output is: "ArrayIndex still negative after conversion to unsigned
int -1"

How is this possible?

I would expect a output 129.

Thanks!

Hans
Jul 23 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
Hans wrote:
Hello,

Thanks for all your response on my question about signed/unsigned chars.

The problem is this: I want to use the char array for a ArrayLookup, so
I need to convert char to unsigned int.

But this doesn't work!

If I try this:

#include <stdio.h>
int main() {
char c = -1; // 129 unsigned and -1 signed right?
Wrong.

// convert it to a value between 0 - 255 for array Lookup
unsigned int arrayIndex = (unsigned int) c;

The casting is not needed.

Also this is equivalent to

unsigned int arrayIndex = numeric_limits<unsigned int>::max();
or UINT_MAX of <climits>.


if (arrayIndex < 0)
printf("ArrayIndex still negative after conversion to unsigned int
%d\n",arrayIndex);
else
printf("Unsigned representation is OK for ArrayIndex=%d\n",arrayIndex);
}

The output is: "ArrayIndex still negative after conversion to unsigned
int -1"

How is this possible?

%d is for int, %u is for unsigned int.
I just noticed you are cross posting to clc, usually a bad thing to do
since C and C++ are different languages.
Anyway your program fixed and being valid C99 and C++03:
#include <stdio.h>
#include <limits.h>

int main()
{
char c = -1;

unsigned int arrayIndex = c;

if (arrayIndex < 0)
printf("ArrayIndex still negative after conversion to unsigned
int %u\n",arrayIndex);

else
printf("Unsigned representation is OK for
ArrayIndex=%u\n\n",arrayIndex);
printf("UINT_MAX is %u\n", UINT_MAX);
}


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #2

P: n/a
Hans wrote:
The output is: "ArrayIndex still negative after conversion to unsigned
int -1"

How is this possible?

Still, the original erroneous code did not print the above.


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #3

P: n/a
Hans wrote:
Hello,

Thanks for all your response on my question about signed/unsigned chars.

The problem is this: I want to use the char array for a ArrayLookup, so
I need to convert char to unsigned int.

But this doesn't work!

If I try this:

#include <stdio.h>
int main() {
char c = -1; // 129 unsigned and -1 signed right?
Wrong. c is either -1 or UCHAR_MAX (for CHAR_BIT==8: 255)
// convert it to a value between 0 - 255 for array Lookup
Wrong. You either get UCHAR_MAX or UINT_MAX from the following
unsigned int arrayIndex = (unsigned int) c;
Probably, you want to have ... = (unsigned char) c;
Otherwise, the cast is unnecessary.
if (arrayIndex < 0)
printf("ArrayIndex still negative after conversion to unsigned int
%d\n",arrayIndex); %u; you are lying about the type, so printf() will get it wrong. else
printf("Unsigned representation is OK for ArrayIndex=%d\n",arrayIndex); Dito. }

The output is: "ArrayIndex still negative after conversion to unsigned
int -1"
You are lying. The output of the above probably is
"Unsigned representation is OK for ArrayIndex=-1\n"
Anything else is not possible.
How is this possible?
It ain't.
I would expect a output 129.
I would expect for CHAR_BIT==8 either 255 or -1 and for the fixed
printf() format string either 255 or UINT_MAX which is probably either
2**16-1 or 2**32-1 (65,XXX or 4,XXX,XXX,XXX).

See also the replies of Ioannis Vranos.

Please get decided whether to post in clc or clc++.
F'up2 clc
Thanks!


You're welcome.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jul 23 '05 #4

P: n/a
Hans <Ha**@replyingrouptoavoidspam.com> writes:
[...]
If I try this:

#include <stdio.h>
int main() {
char c = -1; // 129 unsigned and -1 signed right?
// convert it to a value between 0 - 255 for array Lookup
unsigned int arrayIndex = (unsigned int) c;
if (arrayIndex < 0)
printf("ArrayIndex still negative after conversion to unsigned int
%d\n",arrayIndex);
else
printf("Unsigned representation is OK for ArrayIndex=%d\n",arrayIndex);
}

The output is: "ArrayIndex still negative after conversion to unsigned
int -1"


Why did you cross-post to comp.lang.c and comp.lang.c++? There's
nothing specific to C++ in your program (except possibly the comments
and the missing return statement). Remember, C and C++ are two
different languages.

It's best to use /*...*/ comments when posting to comp.lang.c. Not
all C compilers support // comments, and they're more vulnerable to
being split across lines by news software.

You should also keep your source lines relatively short. The string
literal in your first printf() was split.

Assuming CHAR_BIT==8 (and UCHAR_MAX==255), the unsigned equivalent of
-1 is 255, not 129.

The cast on the initialization of ArrayIndex is superfluous; the value
is going to be converted to unsigned int anyway.

You should have a "return 0;" at the end of your main program. It's
not strictly required in C99 or C++, but it is in C90 (if you don't
want to return an undefined exit status).

You're using the wrong printf format for arrayIndex. The variable is
of type unsigned int; you need to use "%u", not "%d".

Having said all that, I can't reproduce the problem you describe. The
value assigned to arrayIndex is going to change depending on whether
"plain" char is signed or unsigned. If it's unsigned, c is set to
UCHAR_MAX (typically 255), and arrayIndex is set to the same value.
If plain char is signed, c is set to -1, and arrayIndex is set to
UINT_MAX (typically 4294967295 on 32-bit systems) -- which is likely
to be printed as "-1" if you use the incorrect "%d" format. (I think
C++ behaves the same way as C.)

If you want to use a char to store a value that has to be
non-negative, use an unsigned char, not a plain char. Plain char may
happen to work on your system, but your code is likely to break on
others (and it's more difficult to determine what the code is going to
do if you leave the signedness up to the compiler).

By using (unsigned) char as an array index, you're limiting the index
values to 255 (potentially more on exotic systems with CHAR_BIT > 8,
but 255 is the portable limit). Not knowing anything about your
application, I can't guess whether that's a potential problem.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 23 '05 #5

P: n/a
Followup-to: header ignored.

Michael Mair <Mi**********@invalid.invalid> writes:
Hans wrote:

[...]
The output is: "ArrayIndex still negative after conversion to
unsigned int -1"


You are lying. The output of the above probably is
"Unsigned representation is OK for ArrayIndex=-1\n"
Anything else is not possible.


I don't see how the posted program could print what Hans says it does,
but it's not necessary to accuse him of lying. He might have mis-read
the output, he might have posted code that didn't match what he
actually compiled, or he might even have a buggy compiler.

If he's wrong, say so, but we don't know enough to assume that he's
being deliberately dishonest.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 23 '05 #6

P: n/a

Hans wrote:
Hello,

Thanks for all your response on my question about signed/unsigned chars.
The problem is this: I want to use the char array for a ArrayLookup, so I need to convert char to unsigned int.

But this doesn't work!

If I try this:

#include <stdio.h>
int main() {
char c = -1; // 129 unsigned and -1 signed right?
// convert it to a value between 0 - 255 for array Lookup
unsigned int arrayIndex = (unsigned int) c;
if (arrayIndex < 0)
printf("ArrayIndex still negative after conversion to unsigned int %d\n",arrayIndex);
else
printf("Unsigned representation is OK for ArrayIndex=%d\n",arrayIndex); }

The output is: "ArrayIndex still negative after conversion to unsigned int -1"

How is this possible?

I would expect a output 129.

Thanks!

Hans


- specifier for unsigned int is %u, not %d
- always return 0 before the final brace of main
- your expected output of 129 is unreasonable, you're either going to
get UCHAR_MAX or UINT_MAX.
- the statement after else should always execute, because an unsigned
int is never less than 0
- either you are lying (which I doubt), your compiler is wrong, or the
undefined behaviour your original code falls into affected your results

Jul 23 '05 #7

P: n/a
Keith Thompson wrote:
Followup-to: header ignored.

Michael Mair <Mi**********@invalid.invalid> writes:
Hans wrote:


[...]
The output is: "ArrayIndex still negative after conversion to
unsigned int -1"


You are lying. The output of the above probably is
"Unsigned representation is OK for ArrayIndex=-1\n"
Anything else is not possible.

I don't see how the posted program could print what Hans says it does,
but it's not necessary to accuse him of lying. He might have mis-read
the output, he might have posted code that didn't match what he
actually compiled, or he might even have a buggy compiler.

If he's wrong, say so, but we don't know enough to assume that he's
being deliberately dishonest.


You are right, that was not what I intended to say and way too harsh.
I apologize for that.
-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Jul 23 '05 #8

P: n/a
Keith Thompson wrote:
Followup-to: header ignored.

Michael Mair <Mi**********@invalid.invalid> writes:
Hans wrote: [...]
The output is: "ArrayIndex still negative after conversion to
unsigned int -1"


You are lying. The output of the above probably is
"Unsigned representation is OK for ArrayIndex=-1\n"
Anything else is not possible.


I don't see how the posted program could print what Hans says it

does, but it's not necessary to accuse him of lying. He might have mis-read the output, he might have posted code that didn't match what he
actually compiled, or he might even have a buggy compiler.

If he's wrong, say so, but we don't know enough to assume that he's
being deliberately dishonest.

I don't think Mike meant the OP was truly lying for the purpose of
confusing c.l.c readers. He probably meant it as if he was suprised,
kind of like if someone says something unbelievable you might respond:
"No way, you're lying."

He did further go on to help the OP, so lighten up Keith. If you're
going to be such a hard a*s about every post on c.l.c. that is even a
little OT, incorrect or improper, then lead by example.

In the thread titled: "Do you like my strlcpy strlcat etc?" you join in
with other posters to mock the use of symabolic constants by the OP.
The OP didn't deserve that and everyone who responded just made fun of
him/her. You took part in a session where people kept making worse
versions of the original preprocessor defines. Not everyone has been
roaming c.l.c and programming in C for ages, so lighten up "a little"
or apply the same strict rules you enforce on yourself.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst> San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst> We must do something. This is something. Therefore, we must do this.


Jul 23 '05 #9

P: n/a
Ioannis Vranos wrote:
The casting is not needed.

Also this is equivalent to

unsigned int arrayIndex = numeric_limits<unsigned int>::max();
or UINT_MAX of <climits>.

Actually this is the case when char is signed.
--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #10

P: n/a
"TTroy" <ti*****@gmail.com> writes:
[...]
I don't think Mike meant the OP was truly lying for the purpose of
confusing c.l.c readers. He probably meant it as if he was suprised,
kind of like if someone says something unbelievable you might respond:
"No way, you're lying."

He did further go on to help the OP, so lighten up Keith. If you're
going to be such a hard a*s about every post on c.l.c. that is even a
little OT, incorrect or improper, then lead by example.
Read Mike's reaponse (which you probably hadn't seen when you posted
yours).
In the thread titled: "Do you like my strlcpy strlcat etc?" you join in
with other posters to mock the use of symabolic constants by the OP.
The OP didn't deserve that and everyone who responded just made fun of
him/her. You took part in a session where people kept making worse
versions of the original preprocessor defines. Not everyone has been
roaming c.l.c and programming in C for ages, so lighten up "a little"
or apply the same strict rules you enforce on yourself.


Yes, quite frankly, the OP did deserve to be mocked. He's been told
many times before, over several years, that his macros serve only to
make his code more difficult to read. He doesn't seem to care whether
he's actually communicating, which makes many of us wonder why he
wastes his time (and ours) by posting here. Perhaps I should have
just ignored it, but in this case I chose not to.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 23 '05 #11

P: n/a
TTroy wrote:
.... snip ...
In the thread titled: "Do you like my strlcpy strlcat etc?" you
join in with other posters to mock the use of symabolic constants
by the OP. The OP didn't deserve that and everyone who responded
just made fun of him/her. You took part in a session where people
kept making worse versions of the original preprocessor defines.
Not everyone has been roaming c.l.c and programming in C for ages,
so lighten up "a little" or apply the same strict rules you
enforce on yourself.


Yes he did deserve it. This is far from the first time he has been
told about it. The best use for trolls is as butts for jokes.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Jul 23 '05 #12

P: n/a
On Sat, 05 Feb 2005 23:35:27 GMT, CBFalconer <cb********@yahoo.com>
wrote:
TTroy wrote:

... snip ...

In the thread titled: "Do you like my strlcpy strlcat etc?" you
join in with other posters to mock the use of symabolic constants
by the OP. The OP didn't deserve that and everyone who responded
just made fun of him/her. You took part in a session where people
kept making worse versions of the original preprocessor defines.
Not everyone has been roaming c.l.c and programming in C for ages,
so lighten up "a little" or apply the same strict rules you
enforce on yourself.


Yes he did deserve it. This is far from the first time he has been
told about it. The best use for trolls is as butts for jokes.


I had thought on str[l]cpy str[l]cat
there is no need of strcpy, strcat they are easily emulate
with some loop (so strcpy strcat are errors)
For me your str[l]cpy has at last one error
if p is a pointer to char and sizeof(array pointed from p)=sz
if the array of chars poited from p have no 0 in the first sz-1
positions => str[l]cpy has to end it with a 0 in the sz-1 position.

I think strlcpy strlcat are too errors
I think there is only the need
sprintf for fast and accurate programming (that can emulate strcpy and
strcat)
and something like
snprintf(char* buffer, size_t size, char* fmt, ...); (that can emulate
strlcpy strlcat)
for everyday functions

the same for malloc:
one malloc for fast and accurate programming and a malloc2 that can
find the errors in memory for write out of bouds etc for everyday
functions

Then if you like a buggy language ...

Jul 23 '05 #13

P: n/a
On Sat, 05 Feb 2005 22:28:10 GMT, Keith Thompson <ks***@mib.org>
wrote:
"TTroy" <ti*****@gmail.com> writes:
[...]
I don't think Mike meant the OP was truly lying for the purpose of
confusing c.l.c readers. He probably meant it as if he was suprised,
kind of like if someone says something unbelievable you might respond:
"No way, you're lying."

He did further go on to help the OP, so lighten up Keith. If you're
going to be such a hard a*s about every post on c.l.c. that is even a
little OT, incorrect or improper, then lead by example.
Read Mike's reaponse (which you probably hadn't seen when you posted
yours).
In the thread titled: "Do you like my strlcpy strlcat etc?" you join in
with other posters to mock the use of symabolic constants by the OP.
The OP didn't deserve that and everyone who responded just made fun of
him/her. You took part in a session where people kept making worse
versions of the original preprocessor defines. Not everyone has been
roaming c.l.c and programming in C for ages, so lighten up "a little"
or apply the same strict rules you enforce on yourself.


Yes, quite frankly, the OP did deserve to be mocked. He's been told
many times before, over several years, that his macros serve only to
make his code more difficult to read. He doesn't seem to care whether
he's actually communicating, which makes many of us wonder why he
wastes his time (and ours) by posting here.


If you lost your time reading my message kill file my messages
but I think these macro are Right and ok because read these macro is
more easy for me.
Perhaps I should have
just ignored it, but in this case I chose not to.


Jul 23 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.