Thread: Alternative to switch/case?

  1. #1
    Registered User
    Join Date
    Jan 2006

    Question Alternative to switch/case?

    Hi I'm new to C and was wondering whether there is a better way to solve my problem than a switch( ) case statment.

    I'm trying to decode a byte of data from an ICMP packet, because it's a byte, it can have 256 different values, with most of the values relating to a different piece of content. Currently i am using this method:

    switch (value)
      case 0:    printf("0 echo reply");
      case 1:    printf("1 unassigned");
      case 2:    printf("2 unassigned");
      case 3:    printf("3 destination unreachable")
      case 4;    printf("4 source quench")
    ..... and so on and so on......... until 255
    As you can imagine this method is quite time consuming, is there a better/more efficient way to do this?

    Any help would be greatly appreciated

  2. #2
    Rabble Rouser Slacker's Avatar
    Join Date
    Dec 2005
    const char *content[] = {
      "echo reply",
      "destination unreachable",
      "source quench"
      ... and so on until 255
    printf ( "%d %s", value, content[value] );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alternative to malloc
    By stellastarr in forum C Programming
    Replies: 13
    Last Post: 04-30-2007, 04:10 PM
  2. ASP.NET alternative to ???
    By gicio in forum C# Programming
    Replies: 0
    Last Post: 05-15-2005, 11:31 AM
  3. Alternative to fstream
    By golfinguy4 in forum Windows Programming
    Replies: 3
    Last Post: 07-03-2002, 01:51 AM
  4. alternative to cin
    By Unregistered in forum C++ Programming
    Replies: 18
    Last Post: 06-25-2002, 05:14 PM
  5. ansi alternative to this?
    By Ragman in forum C Programming
    Replies: 4
    Last Post: 06-13-2002, 01:58 PM