Welcome, Guest. Please login or register.

Author Topic: multi-character character constants in C++  (Read 5148 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline SidewinderTopic starter

  • Full Member
  • ***
  • Join Date: Mar 2002
  • Posts: 241
    • Show only replies by Sidewinder
    • http://www.liquido2.com
multi-character character constants in C++
« 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,
Sidewinder
 

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: multi-character character constants in C++
« Reply #1 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.
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: multi-character character constants in C++
« Reply #2 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 :-)
int p; // A
 

Offline SidewinderTopic starter

  • Full Member
  • ***
  • Join Date: Mar 2002
  • Posts: 241
    • Show only replies by Sidewinder
    • http://www.liquido2.com
Re: multi-character character constants in C++
« Reply #3 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.
Sidewinder
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: multi-character character constants in C++
« Reply #4 on: April 06, 2003, 12:48:30 AM »
Ah, I see your dilemma.

Maybe you can disable unsafe conversion warnings in the compiler options?
int p; // A
 

Offline elendil

  • Sr. Member
  • ****
  • Join Date: Nov 2002
  • Posts: 324
    • Show only replies by elendil
    • http://www.idiot.fnuck.dk
Re: multi-character character constants in C++
« Reply #5 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.
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: multi-character character constants in C++
« Reply #6 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...
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: multi-character character constants in C++
« Reply #7 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..
int p; // A
 

Offline mdwh2

  • Hero Member
  • *****
  • Join Date: Jun 2002
  • Posts: 565
    • Show only replies by mdwh2
Re: multi-character character constants in C++
« Reply #8 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.
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: multi-character character constants in C++
« Reply #9 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?
int p; // A
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: multi-character character constants in C++
« Reply #10 on: April 06, 2003, 03:08:59 AM »
Hmm....



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



Better? Or no change?
int p; // A
 

Offline Rodney

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 1386
    • Show only replies by Rodney
    • http://donthaveone.com/
Re: multi-character character constants in C++
« Reply #11 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 ?
We are not Humans having a spirital experiance
We are Spirits having a Human experiance.
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: multi-character character constants in C++
« Reply #12 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)...
int p; // A
 

Offline elendil

  • Sr. Member
  • ****
  • Join Date: Nov 2002
  • Posts: 324
    • Show only replies by elendil
    • http://www.idiot.fnuck.dk
Re: multi-character character constants in C++
« Reply #13 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.
 

Offline Karlos

  • Sockologist
  • Global Moderator
  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 16879
  • Country: gb
  • Thanked: 5 times
    • Show only replies by Karlos
Re: multi-character character constants in C++
« Reply #14 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
int p; // A