Thread: Error msg regarding the Switch loop, What's wrong in my code?

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    14

    Question Error msg regarding the Switch loop, What's wrong in my code?

    Hi there,

    This is my code
    Code:
    int SRV_TYPE=0;
    const int HTTP=80;
    const int FTP=21;
    const int TELNET=23;
    const int POP3=110;
    const int SMTP=25;
    ..
    .
    .
    .
    SRV_TYPE= (int)DST_PORT;
    switch (SRV_TYPE)
      {	case HTTP:	printf("HTTP Data ..... \n");
      			print_data(tcp_ptr,length);
      			break;
      			
    	case FTP:	printf("FTP Data .....\n");
    			print_data(tcp_ptr,length);
    			break;
    			
    	case TELNET:	printf("TELNET Data ....\n");
    			print_data(tcp_ptr,length);
    			break;
    	
    	case POP3:	printf("POP3 Data ....\n");
    			print_data(tcp_ptr,length);
    			break;
    	
    	case SMTP:	printf("SMTP Data ....\n");
    			print_data(tcp_ptr,length);
    			break;
    	
    	default :	printf("Unknown Data Type\n");
    			break;
      }
    When i compile it on FreeBSD, I get this error msg :
    Code:
    In function `print_tcp_seg':
    463: case label does not reduce to an integer constant
    467: case label does not reduce to an integer constant
    471: case label does not reduce to an integer constant
    475: case label does not reduce to an integer constant
    479: case label does not reduce to an integer constant
    463: warning: unreachable code at beginning of switch statement
    *** Error code 1

    It seems the problem with the SWITCH statement, I am gessing that there is a mismatch between SRV_TYPE (int) and DST_PORT (u_short), am I right ?
    SRV_TYPE=(int) DST_PORT; CAN I DO THAT ????

    Any suggestion ?

    any tiny help is very highly appreciated.

    AHT

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try enumerations instead of "const int":
    Code:
    enum { HTTP = 80, FTP = 21, TELNET = 23, POP3 = 110, SMTP = 25 };
    The typecast is fine, and if you'll pay attention to the actual warnings, it has nothing to do with that line.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    14
    What about this line :
    SRV_TYPE= (int)DST_PORT;

    is it legal in C ?! and does c treat SRV_TYPE as an intger with the value of DST_PORT (0x50)? , which is 80 .

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    14
    Follow up:
    Is there any way in c to check for a variable's value if it is in the enum { HTTP = 80, FTP = 21, TELNET = 23, POP3 = 110, SMTP = 25 }; ?

    something like this :
    if (service in enum) do something....

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    is it legal in C ?!
    Of course it's legal. It's a typecast. If in doubt, turn on your compiler warnings, and try "-pedantic", and optionally "-ansi" when you compile.

    You could always try it and see, you know:
    Code:
    printf("SRV_TYPE = (int)DST_PORT gives us %d\n", (SRV_TYPE = (int)DST_PORT) );
    Is there any way in c to check for a variable's value if it is in the enum { HTTP = 80, FTP = 21, TELNET = 23, POP3 = 110, SMTP = 25 }; ?
    Of course you can.
    Code:
    int x = 21;
    
    if( x == FTP )
        printf("x is set to FTP\n" );
    else
        printf("x is set to %d\n", x );
    You shouldn't feel afraid to try something. It'll either work or it won't. Either way, perhaps you'll learn something from it.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Quote Originally Posted by althagafi
    Follow up:
    Is there any way in c to check for a variable's value if it is in the enum { HTTP = 80, FTP = 21, TELNET = 23, POP3 = 110, SMTP = 25 }; ?

    something like this :
    if (service in enum) do something....
    No, but you can write your own function.
    Code:
    int is_in (int *array, size_t nb, int value)
    {
       /* ... */
    }
    Emmanuel Delahaye

    "C is a sharp tool"

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Emmanuel Delaha
    No, but you can write your own function.
    Code:
    int is_in (int *array, size_t nb, int value)
    {
       /* ... */
    }
    I read that backwards... I figured they wanted to compare to an enum.

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jul 2004
    Posts
    14
    Thanx to all.
    I got it running fast.

    Please, close this thread with high appreciation to all kinds of help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. loop and compiling inside a code
    By MtJ in forum C Programming
    Replies: 3
    Last Post: 03-05-2006, 12:50 PM
  3. Posting Code == Wrong?
    By andyhunter in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 01-27-2005, 07:53 PM
  4. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  5. can (switch) be apart of a loop?
    By matheo917 in forum C++ Programming
    Replies: 2
    Last Post: 09-20-2001, 06:29 PM