473,699 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Choosing the right tabel through a switch statement

4 New Member
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.
Mar 2 '08 #1
7 1847
Kristiansj
4 New Member
Table should be 'struct', sorry about that.
Mar 3 '08 #2
hsn
237 New Member
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
Mar 3 '08 #3
Kristiansj
4 New Member
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 :)
Mar 3 '08 #4
mac11
256 Contributor
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?
Mar 3 '08 #5
Kristiansj
4 New Member
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.
Mar 3 '08 #6
Laharl
849 Recognized Expert Contributor
You can use the address-of operator, &.

Expand|Select|Wrap|Line Numbers
  1. typdef struct foo {
  2.   int bar;
  3. } foo;
  4.  
  5. ...
  6. int a = rand()%10; //a is a random number 0-9
  7. foo *f;
  8. switch(a){
  9.   case 1:
  10.     f = &struct_1;
  11.     break;
  12.    ...
  13.   default:
  14.      f = &default_struct;
  15. }
  16. //do stuff with f as your pointer, using -> to access its data
  17.  
Mar 3 '08 #7
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Mar 3 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

10
10905
by: Myster Ious | last post by:
Polymorphism replaces switch statements, making the code more compact/readable/maintainable/OO whatever, fine! What I understand, that needs to be done at the programming level, is this: a switch-case has a variable (most probably an enumeration) & associated symbols or integral value. Selection is made, base on what symbol/value the variable holds. So
35
8338
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except for '5'. So in his switch statement, he omits a case for '5':
5
6102
by: Alvin Bruney | last post by:
Is a switch more efficient than an if statement? I observe thru the debugger that a switch statement jumps directly to its case handler where as an if statement examines all conditions sequentially. Is that a trick of the debugger or is a switch quicker by O(n).
3
19737
by: pgraeve | last post by:
I am a convert from VB to C# so bear with me on this "conversion" question C# switch statement seems to be the closest relative to VB's Select Case. I used VB's Select Case statement liberally. Now I find myself wanting to use "Select Case" i.e., "switch" in C# regularly, but I always have to find another way b/c C#'s switch statement only allows static or integral variables. For example, I often want to use a switch statement based on the...
25
18379
by: v4vijayakumar | last post by:
'continue' within switch actually associated with the outer 'while' loop. Is this behavior protable? int ch = '\n'; while (true) { switch(ch) { case '\n': cout << "test"; continue; } }
12
12332
by: | last post by:
Is it fine to call another method from Switch? Eg. Switch (stringVar) { case ("a"): somVar = "whatever"; Another_Method(); //call another method return;
4
1625
by: PengYu.UT | last post by:
Hi, I have two questions. 1. Are there any good way to generalize the code for switch statement with any number of cases? 2. Suppose test() is a very big function, and there are many cases in the switch statement. The binary code could be huge. Are there any method to deal with this problem.
2
1451
by: Phillip B Oldham | last post by:
What would be the optimal/pythonic way to subject an object to a number of tests (based on the object's attributes) and redirect program flow? Say I had the following: pets = {'name': 'fluffy', 'species': 'cat', 'size': 'small'} pets = {'name': 'bruno', 'species': 'snake', 'size': 'small'} pets = {'name': 'rex', 'species': 'dog', 'size': 'large'}
13
11814
by: Satya | last post by:
Hi everyone, This is the first time iam posting excuse me if iam making any mistake. My question is iam using a switch case statement in which i have around 100 case statements to compare. so just curious to find out is it effective to use this method?? or is there is any other alternative method present so that execution time and code size can be reduced?? Thanks in advance.
0
9174
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9034
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8914
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8883
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7750
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6534
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5874
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4376
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4629
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.