Choosing the right tabel through a switch statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Kristiansj
    New Member
    • Mar 2008
    • 4

    Choosing the right tabel through a switch statement

    Hey, I am a fairly novice coder and I am having some trouble making an elegant solution to the following problem: (pseudocode)

    ------------
    switch (something)
    {
    case (something1)
    tabel = tabel_1
    case (something2)
    tabel = tabel_2
    etc....
    }

    number = tabel[some int].number;
    .....
    ------------
    A solution would of course be just to have
    case (something1)
    number = tabel_1[some int].number

    but there is a lot of code that has to be at every case in the switch making it very repeditive and long, and not as elegant as I hoped for.


    So I guess my question is;
    Is there a way to 'point' to a tabel that was previously decided on in a switch statement ?

    Thanks for your time.
  • Kristiansj
    New Member
    • Mar 2008
    • 4

    #2
    Table should be 'struct', sorry about that.

    Comment

    • hsn
      New Member
      • Sep 2007
      • 237

      #3
      you can create a function that will be called when ever you come to a case in your switch.
      that will decrease the size of your code .
      so when ever you come to a case you just call the function

      hope that helps

      regards....hsn

      Comment

      • Kristiansj
        New Member
        • Mar 2008
        • 4

        #4
        Originally posted by hsn
        you can create a function that will be called when ever you come to a case in your switch.
        that will decrease the size of your code .
        so when ever you come to a case you just call the function

        hope that helps

        regards....hsn
        Yeah that would be a solution. Just kinda hoped there was an easy way to simply direct a pointer to a different struct depending on the case in the switch. Then after the switch use that pointer to get the right struct, thus having it all in one function.

        Thanks for the reply, I think I'll do what you suggested, for now at least :)

        Comment

        • mac11
          Contributor
          • Apr 2007
          • 256

          #5
          Originally posted by Kristiansj
          Just kinda hoped there was an easy way to simply direct a pointer to a different struct depending on the case in the switch. Then after the switch use that pointer to get the right struct, thus having it all in one function.
          This topic is interesting to me because I hate it when I can't structure my code the way I want to.

          I'd like to help, but I think maybe I'm missing something... What exactly is preventing you from doing it the way you want? Can you post a source code example?

          Why not set the pointer in the switch and then do all the "repetitive " stuff (as you state in your first post) after the switch? What's the "repetitive " stuff?

          Comment

          • Kristiansj
            New Member
            • Mar 2008
            • 4

            #6
            Hehe...well it is probably doable, and I don't really have any sourcecode atm, but I can write some pseudocode that hopefully will explain it:

            int x,y,z;

            x = number_range(1, 10);


            switch( x )
            {
            default:
            struct_to_be_us ed == struct_default
            case 1:
            struct_to_be_us ed == struct_1
            case 2:
            struct_to_be_us ed == struct_2
            etc.etc
            }

            /*After we have found the correct struct, depending on x we can finally use that*/

            y = struct_to_be_us ed[something].integerY;
            z = struct_to_be_us ed[something].integerZ;
            ..... more code using the struct we found....

            Now of course this could be done as:
            switch( x )
            {
            default:
            y = struct_default[something].integerY;
            z = struct_default[something].integerZ;
            ..... more code using struct_default. ...
            case 1:
            y = struct_1[something].integerY;
            z = struct_1[something].integerZ;
            ..... more code using struct_1....
            case 2:

            etc etc..
            But this gives quite a lot of code compared to, somehow, just make a "struct_to_be_u sed" and then have that point to the proper struct via the switch statement, thus only having to have the switch statement, and then the block of code ones instead of inserted into each of the cases in the switch statement.

            I hope that made a bit of sense.

            Why not set the pointer in the switch and then do all the "repetitive " stuff (as you state in your first post) after the switch? What's the "repetitive " stuff?
            Well... that is basically what I want to do... I just don't know how to make a pointer point to a specific struct. Could you give an example or perhaps a reference ? Pointers aren't my best friends sadly.

            Comment

            • Laharl
              Recognized Expert Contributor
              • Sep 2007
              • 849

              #7
              You can use the address-of operator, &.

              [CODE=c]
              typdef struct foo {
              int bar;
              } foo;

              ...
              int a = rand()%10; //a is a random number 0-9
              foo *f;
              switch(a){
              case 1:
              f = &struct_1;
              break;
              ...
              default:
              f = &default_struct ;
              }
              //do stuff with f as your pointer, using -> to access its data
              [/CODE]

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by Kristiamsj
                Yeah that would be a solution. Just kinda hoped there was an easy way to simply direct a pointer to a different struct depending on the case in the switch. Then after the switch use that pointer to get the right struct, thus having it all in one function.
                Fire up your Google and research a thing called a discriminated union. Microsoft uses this in a thing called a VARIANT. Code for this is on the Internet.

                Essentially, you embed tyhe address of your struct variable inside the VARIANT struct along with a second value (the discriminator) so you can determine what type of address was put in the VARIANT. Then all your code uses VARIANT.

                BTW: Do not do this in C++. What you are asking about is a C problem. C++ uses other means for this.

                Comment

                Working...