Amiga.org

Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: Sidewinder on April 05, 2003, 10:08:31 PM

Title: multi-character character constants in C++
Post by: Sidewinder on April 05, 2003, 10:08:31 PM
I have some code that I'm trying to port using StormC 4.  It compiles for the most part, but I get litterally thousands of warnings about multi-character character constants.  What I have is basically somthing like this:

enum
{

JOHN_NAME = 'john',
FRED_NAME = 'fred',
MIKE_NAME = 'mike',
ANNE_NAME = 'anne'

}

I gather that since enums are 4 byte integers each character in the character constant is supposed to be copied to the corresponding byte in the enum.  So you have essentially an array of 4 bytes that is cast as an int.

Is there any other way to express this that would avoid giving me so many warnings and errors?

Thanks,
Title: Re: multi-character character constants in C++
Post by: Piru on April 05, 2003, 10:28:26 PM
Your code makes an assumptation that int is 32bit. If you can live with this assumptation, then:

#define mkid(a,b,c,d) ((a)<<24 | (b)<<16 | (c)<<8 | (d))

enum
{

JOHN_NAME = mkid('j','o','h','n'),
FRED_NAME = mkid('f','r','e','d'),
...

};

However, one can wonder if this makes any sense at all. Why not just have indexes from 0...n instead?

You can have the names as strings in another indexed table.
Title: Re: multi-character character constants in C++
Post by: Karlos on April 06, 2003, 12:02:52 AM
Hi dude,

This code isn't portable and makes some assumptions which are reasons for your compiler to complain

1) ints aren't always 32 bits (not such a big deal for most platforms)

2) Under C++, enumerations don't have to be ints - the implementation is free to decide the best size for a given enumeration - not really a problem in your code since the logical size would need to be 4-bytes in this case

3) Multicharacter constants are not uniformly supported. How they map to integers may additionally depend on endian issues that are obviously hardware dependent.

If the actual value of your constants isn't imporant to you (as long as theyre unique), just use a normal enumeration which ensures each constant has a unique value.
If you want to enforce an enumeration size to make sure it's 32-bits, you could use something like

enum {
   JOHN_NAME,
   FRED_NAME,
   MIKE_NAME,
   ANNE_NAME,
   DUMMY_NAME = 0xFFFFFFFF
};


Another option to generate a unique number from a string name is to use a hash function.

Here's one I made earlier :-)


unsigned long GetHashValue(const char* text)
{
   unsigned long v = 0;
   unsigned char* p = (unsigned char*)text;
   while(*p) {
      /* bitwise rotate and xor with byte */
      v = (v<<1) | ((v&0x80000000UL) ? 1UL : 0);
      v ^= (unsigned long) (*p++);
   }
   return v;
}


This generates different values for different strings and has lots of uses.

For a slightly off topic example, I use the above (in a class form)  to generate integers from filenames as part of a cacheing system - rather than looking through a list of names (using strcmp()), to see if something is loaded, I generate the hash for each loaded object and search by hash value. Much faster :-)
Title: Re: multi-character character constants in C++
Post by: Sidewinder on April 06, 2003, 12:44:02 AM
You see, the problem is that it's not my code.  It's part of the POVRay ray tracing package which is supposed to be portable code.  There are litterally thousands of occurences of this error.  I guess I'll just ignore it and see what happens.
Title: Re: multi-character character constants in C++
Post by: Karlos on April 06, 2003, 12:48:30 AM
Ah, I see your dilemma.

Maybe you can disable unsafe conversion warnings in the compiler options?
Title: Re: multi-character character constants in C++
Post by: elendil on April 06, 2003, 02:09:55 AM
Karlos:

I have been looking at recent threads and see, code examples by you, but there is one (probably simple) thing I cannot understand.

What does " " do in your code? I have never seen any c-code with that. I always thought it was some htlm-thingie (that I dont know about either).

Sincerely,

-Kenneth Straarup.
Title: Re: multi-character character constants in C++
Post by: Karlos on April 06, 2003, 02:38:59 AM
Quote

elendil wrote:

What does " " do in your code? I have never seen any c-code with that. I always thought it was some htlm-thingie (that I dont know about either).



Pardon? I don't recall that anywhere. What browser are you using?

-edit-

Do you mean single quote as in 'c'? That's a character constant...
Title: Re: multi-character character constants in C++
Post by: Karlos on April 06, 2003, 02:52:50 AM
Hi all,

Is anybody else seeing suspicious "" in my code? I'm not sure what elendil means...

Could it be browser nonsense?

I'd hate to mislead people with tons of nonsense syntax..
Title: Re: multi-character character constants in C++
Post by: mdwh2 on April 06, 2003, 02:58:36 AM
Quote

What does " " do in your code? I have never seen any c-code with that. I always thought it was some htlm-thingie (that I dont know about either).



Pardon? I don't recall that anywhere. What browser are you using?
[/quote]I think he means the "&nbsp". Which is presumably some HTML thing that's gone wrong, since "&nbsp;" is the code for a space. Which also explains why him asking the question didn't make sense, since there it is shown as a space - even though it still didn't have the semicolon, following it immediately with "&quot;" seems to make browsers interpret the code.

Hmm, also just noticed a bug with this website: When doing a preview, it replaces "&amp;" with "&" in the edit box.
Title: Re: multi-character character constants in C++
Post by: Karlos on April 06, 2003, 03:01:26 AM
Ah, that could be it, I suppose. The thing is that code without indentation is a pain in the ass to look at.

Ideas?
Title: Re: multi-character character constants in C++
Post by: Karlos on April 06, 2003, 03:08:59 AM
Hmm....



void test()
{
   printf("How's it look now?\n");
}



Better? Or no change?
Title: Re: multi-character character constants in C++
Post by: Rodney on April 06, 2003, 07:11:36 AM
Quote

elendil wrote:
Karlos:

I have been looking at recent threads and see, code examples by you, but there is one (probably simple) thing I cannot understand.

What does " " do in your code? I have never seen any c-code with that. I always thought it was some htlm-thingie (that I dont know about either).

Sincerely,

-Kenneth Straarup.


Thats an HTML encoding for a non-breaking space. Lord knows how it got into Karlos's code examples. If Karlos is copying and pasting his code, it coudl be working its way in from there. Either way, i dont understand how it could end up as " ".

Maybe Xoops is changeing  &nbsp  -> &amp;nbsp ?
Title: Re: multi-character character constants in C++
Post by: Karlos on April 06, 2003, 11:06:19 AM
Im using non breaking spaces to indent the code (reccomended by Tickly) - my guess is that it got screwed up somehow...

In any event, it all looks fine in IE5 (shameful admission of using that browser)...
Title: Re: multi-character character constants in C++
Post by: elendil on April 06, 2003, 11:20:37 AM
heya,

Your last code example looked nice. I am using opera. I agree indentation is a must, but it REALLY makes code hard to read when there is a (let me see íf I can type it without it being interpreted) & n b s p in front of it all - with no space to the first character, mind you :) a variable v looks very weird being called & nbspv, for example.

Anyway, glad I found out it was an error and not some code thingie I had never heard of.

Sincerely,

-Kenneth Straarup.
Title: Re: multi-character character constants in C++
Post by: Karlos on April 06, 2003, 11:23:01 AM
he he,

Maybe I'll waste some time today and go backwards through my posts to obliterate the broken spaces :-D

-edit-

One down...X to go :-D
Title: Re: multi-character character constants in C++
Post by: Karlos on April 06, 2003, 11:43:06 AM
Quote

Sidewinder wrote:
You see, the problem is that it's not my code.  It's part of the POVRay ray tracing package which is supposed to be portable code.  There are litterally thousands of occurences of this error.  I guess I'll just ignore it and see what happens.


If you can be bothered, you could try an explicit cast to long - eg

JOHN_NAME = (long)'john'

That's still not particularly portable but it might just shut the compiler up...

-edit-

Just noticed: long john :lol:
Title: Re: multi-character character constants in C++
Post by: Vincent on April 06, 2003, 02:36:53 PM
Quote

elendil wrote:

Your last code example looked nice. I am using opera. I agree indentation is a must, but it REALLY makes code hard to read when there is a (let me see íf I can type it without it being interpreted) & n b s p in front of it all - with no space to the first character, mind you :) a variable v looks very weird being called & nbspv, for example.


I'm using Opera 7.01 here and all of Karlos' code posts look fine here.  Strange that it's different for you.
Title: Re: multi-character character constants in C++
Post by: mdwh2 on April 06, 2003, 03:10:45 PM
Quote

Vincent wrote:
I'm using Opera 7.01 here and all of Karlos' code posts look fine here.  Strange that it's different for you.

I think he's edited them now :) (I'm using Opera 7.01 and saw the problem, earlier).
Title: Re: multi-character character constants in C++
Post by: Vincent on April 06, 2003, 03:33:52 PM
Quote

mdwh2 wrote:
I think he's edited them now :) (I'm using Opera 7.01 and saw the problem, earlier).


I was reading this post yesterday, before the editing, and everything looked ok in Opera 7.01 then.  I was actually speaking to Karlos on ICQ when elendil posted asking about the " "s.  He checked from IE5, and I looked from Opera 7.01 and Netscape.  Neither of us could see the "s in Karlos' post.

I should have mentioned that before, sorry.
Title: Re: multi-character character constants in C++
Post by: Karlos on April 06, 2003, 03:36:30 PM
Quote

Vincent wrote:
I was reading this post yesterday, before the editing, and everything looked ok in Opera 7.01 then.  I was actually speaking to Karlos on ICQ when elendil posted asking about the " "s.  


:lol: Was he ever! What time did you hit the sack, mate?
Title: Re: multi-character character constants in C++
Post by: Sidewinder on April 06, 2003, 04:58:55 PM
Quote

If you can be bothered, you could try an explicit cast to long - eg

JOHN_NAME = (long)'john'

That's still not particularly portable but it might just shut the compiler up...

-edit-

Just noticed: long john


"long john" Very nice!  Well I tried the cast, but no joy.  It seems that the compiler isn't complaining about the assignment but rather about the fact that there is more than one character between the single quotes.  The code compiles ok however, just with about 3-4 thousand warnings.  :-)
Title: Re: multi-character character constants in C++
Post by: Vincent on April 07, 2003, 12:06:45 AM
Quote

Karlos wrote:
Quote

Vincent wrote:
I was reading this post yesterday, before the editing, and everything looked ok in Opera 7.01 then.  I was actually speaking to Karlos on ICQ when elendil posted asking about the " "s.  


:lol: Was he ever! What time did you hit the sack, mate?


:lol:
I was too hungry to just go to bed, so I had to make something to eat. :-D

My brain was really fried by the time I got to bed, I think it was about 4:30, but I don't know for sure. :-)