Thread: "warning: multi-line character constant"

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    161

    "warning: multi-line character constant"

    The code below works but gives the warning "warning: multi-line character constant" for each character constant I have in the switch-case statement.

    Code:
    int GObject::configure(const ConfEl* conf){
    	while(conf){
    		switch(conf->item.id){
    		case 'posx':
    			obj.pos.x = conf->data.i;
    			break;
    		case 'posy':
    			obj.pos.y = conf->data.i;
    			break;
    		case 'posz':
    			obj.pos.z = conf->data.i;
    			break;
    		case 'flag':
    			flags = conf->data.i;
    			break;
    		case 'obj3':
    			load3db(conf->data.str);
    			break;
    		}
    		conf = conf->next;
    	}
    	return 0;
    }
    conf->item.id is an unsigned integer which represents 4 characters. The code above works as expected. Why do I get those warnings? What can I do to eliminate those warnings?

    Thank you

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How did you assign the integer item.id?
    Some macro with a 4-character string?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You get the warning because it is usually a mistake to use multiple characters as a constant. I'm not quite sure how you go about avoiding the warning - execpt I prefer to use proper constants instead of 'xxxx' for integer values for portability reasons (endian-ness changes the values).

    Which compiler are you using?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    I'm using gcc. For the conf->item.id I want the id's of objects be "names" and integers at the same time. To create the 'id' I parse a file of "name=value" pairs and the first 4 letters are used.

    the actual code:
    Code:
    /* n is of the form "name=value" or "name{"
     * copyName takes the "name" and puts it in i->id/name
     */
    int copyName(ConfItem* i, char* n){
    	int k;
    	static char buf[32];
    	/* Gets the "name" part of "name=value" and stores in buf */
    	getIDPart(buf, n);
    	/* check if the "name" part looks like a hexnumber e.g. "0x3434" */
    	if(isHexStr(buf)){
    		/* use id specified */
    		sscanf(buf+2, "%x", &i->id);
    	} else {
    		/* if not use first 4 letters of "name" */
    		i->id = 0;
    		/* create id from first 4 letters */
    		for(k = 0; k < 4; k++){
    			if(buf[k] == 0)
    				break;
    			/* i->name & i->id share a union: The next line is not portable its either (3-k) or (k)
    			* depending on endianess */
    			i->name[3-k] = buf[k];
    		}
    	}
    	return 0;
    }

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Any chance you wrote the error mesage down wrong here? None of those multi-character constants appear to go over multiple lines. "multi-character" would make more sense.
    You may have to resort to #prgamas or other compiler specific methods to suppress this warning. Or perhaps just an explicit cast will help?

    I don't think how the values were assigned to id is relevent to the error, but Salem is probably looking for a way to avoid using them entirely.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I was thinking along these lines
    Code:
    #define MAKEID(x)   ( (x[0]<<24) | (x[1]<<16) | (x[2]<<8) | (x[3]) )
    
    // Then you can do
    i->id = MAKEID(buf);
    // You might want to make sure buf is padded with \0 out to 4 characters
    
    // You can also do
    case MAKEID("posx"):
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    Yes I made a mistake copying the error, it is "multi-character". Putting the MAKEID macro into the "case" statement gives

    Code:
    		case MAKEID("posx"): ./* Line 8 */
    			obj.pos.x = conf->data.i;
    			break;
    
    // gobject.cpp:8: error: an array reference cannot appear in a constant-expression
    It doesn't seem like gcc pre-calculates the values. Perhaps a perl-script to replace MAKEID's with pre-calculate values? That will start to make my work look "magical" to others though.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Or you give it four parameters with individual character constants.
    Code:
    MAKEID('p', 'o', 's', 'x')
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM
  2. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 04:53 PM
  3. Validating the contents of a char buffer
    By mattz in forum C Programming
    Replies: 3
    Last Post: 12-09-2001, 06:21 PM
  4. End of Line character
    By jloyd01 in forum C Programming
    Replies: 3
    Last Post: 12-02-2001, 05:59 AM
  5. cannot get a character, I need one line
    By steven in forum C Programming
    Replies: 2
    Last Post: 09-18-2001, 05:28 PM