switch expression not integral ???

B

bayxarea-usenet

I am getting the following error during compiling:

'switch expression not integral'

I am new to the switch command - here is a snip of my code where I have
used this.. .. what is the problem here? Thanks!

------------- snip --------------------------

for (ii = 0; ii < n; ii++)
{
if (!UF_OBJ_ask_name(members[ii], obj_name))
{
sprintf(msg,"\nObject #%d has an object tag = %d and is named
%s,",ii,members[ii],obj_name);
WRITE(msg);

UF_CALL(UF_DRF_ask_draft_aid_text_info(members[ii], &nn,
&text_info));

for (iii=0; iii<nn; iii++)
{
WRITE3F(text_info[iii].origin);
for (jj=0; jj<text_info[iii].num_lines; jj++)
{
sprintf(msg, " %s\n", text_info[iii].text[jj].string);
UF_UI_write_listing_window( msg );
}
}
UF_CALL(UF_DRF_free_text(nn, &text_info));
switch (obj_name)
{
case "NOTE TEXT CP SL":
uc5566(members[ii], 1, 6, "J");
break;
case "NOTE TEXT CP COC":
uc5566(members[ii], 1, 6, "O");
break;
case "NOTE TEXT CP PQP":
uc5566(members[ii], 1, 6, "H");
break;
case "NOTE TEXT CP FRD":
uc5566(members[ii], 1, 6, "N);
break;
case "NOTE TEXT CP SC":
uc5566(members[ii], 1, 6, "P);
break;
default:
break;
}

}
}
 
U

Ulrich Eckhardt

I am getting the following error during compiling:

'switch expression not integral'

The argument to switch must be integral. It can't be a floating point
value, a structure or a pointer (including char pointers aka strings).
I am new to the switch command - here is a snip of my code where I have
used this.. .. what is the problem here? Thanks!

Please reduce your examples to the smallest piece of code that demonstrates
your problem.

Uli
 
M

Michael Mair

I am getting the following error during compiling:

'switch expression not integral'

I am new to the switch command - here is a snip of my code where I have
used this.. .. what is the problem here? Thanks!

Well, switch can only be applied to integer data types, i.e
char, (un)signed char, (un)signed short, (un)signed int,
(un)signed long (, size_t, ..., C99: (un)signed long long,
the int*_t types, ptrdiff_t, ...) but not to a pointer
-- at least I guess that obj_name is either of type char *
or of type array of char.

------------- snip --------------------------

for (ii = 0; ii < n; ii++)
{
if (!UF_OBJ_ask_name(members[ii], obj_name))
{
sprintf(msg,"\nObject #%d has an object tag = %d and is named
%s,",ii,members[ii],obj_name);
WRITE(msg);

UF_CALL(UF_DRF_ask_draft_aid_text_info(members[ii], &nn,
&text_info));

for (iii=0; iii<nn; iii++)
{
WRITE3F(text_info[iii].origin);
for (jj=0; jj<text_info[iii].num_lines; jj++)
{
sprintf(msg, " %s\n", text_info[iii].text[jj].string);
UF_UI_write_listing_window( msg );
}
}
UF_CALL(UF_DRF_free_text(nn, &text_info));
switch (obj_name)
{
case "NOTE TEXT CP SL":
uc5566(members[ii], 1, 6, "J");
break;
case "NOTE TEXT CP COC":
uc5566(members[ii], 1, 6, "O");
break;
case "NOTE TEXT CP PQP":
uc5566(members[ii], 1, 6, "H");
break;
case "NOTE TEXT CP FRD":
uc5566(members[ii], 1, 6, "N);
break;
case "NOTE TEXT CP SC":
uc5566(members[ii], 1, 6, "P);
break;
default:
break;
}

}
}

A workaround: Apply switch to the first significant
character...
switch (obj_name[13]) {
case 'S':
/* Either SC or SL */
break;
case 'C':
....
}

Two things for future requests:
* Please give us a minimal example which we can directly
feed to a compiler.
* Indent your code. Not with tabs but with spaces.


Cheers
Michael
 
A

Andrey Tarasevich

I am getting the following error during compiling:

'switch expression not integral'

I am new to the switch command - here is a snip of my code where I have
used this.. .. what is the problem here? Thanks!

The problem here is just what the compiler said: 'switch expression not
integral'. In C language switch argument as well as case labels must be
integral expressions. You seem to be trying to use pointer expressions
('char*') instead. This will not work. If you want to build a multiway
dispatcher based on string argument, one way to go is to use 'if' ladder

if (strcmp(obj_name, "NOTE TEXT CP SL") == 0)
uc5566(members[ii], 1, 6, "J");
else if (strcmp(obj_name, "NOTE TEXT CP COC") == 0)
uc5566(members[ii], 1, 6, "O");
else if (strcmp(obj_name, "NOTE TEXT CP PQP") == 0)
uc5566(members[ii], 1, 6, "H");
/* ... and so on */

Switch is not applicable here. At least, not immediately applicable.
 
B

bayxarea-usenet

Thanks for all the replies - I did not realize it was only a test for
integers - didn't realize that integral meant integer actually.

I will post a more usable snippet in the future...

Thanks again..

John
 
M

Mike Wahler

Thanks for all the replies - I did not realize it was only a test for
integers

Actually, the type of the 'case' expression is even more restricted
than that. The expression must be a constant integer expression.

int main()
{
int x = 42;
int y = 99;

switch(x)
{
case y: /* ERROR: not constant expression */
break;
case 99: /* OK: constant expression */
break;
default:
break;
}

return 0;
}
- didn't realize that integral meant integer actually.

Now you know. :)

-Mike
 
S

Sniper1

Thanks for all the replies - I did not realize it was only a test for
integers - didn't realize that integral meant integer actually.

Where you thinking it meant the area under the switch curve? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top