0

I'm wondering if we can declare something like below. My requirement was to use same variable but different structures. Can you guys help me if the below can be done? Please suggest if there are other options as well.

switch(x)
{
  case 1:
    struct_1 *name = NULL;
    break;
  case 2:
    struct_2 *name = NULL;
    break;
  case 3:
    struct_3 *name = NULL;
    break;
  default:
}

Regards

miikkas
  • 818
  • 1
  • 8
  • 25
  • What is wrong in this code? – Butani Vijay Jul 29 '13 at 13:06
  • possible duplicate of [This](http://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement). – Dayal rai Jul 29 '13 at 13:12
  • I referred that thread before starting this, my query is different and also looking for a possible resolution. – Sheldor Jul 29 '13 at 13:14
  • @Butani Vijay: There is nothing wrong, its just that with this type of declarations he cant have the scope he actually desires –  Jul 29 '13 at 13:24

5 Answers5

5

Case labels don't introduce a new scope. You either have to declare them before the switch with different names, or enclose the code in each case in braces like

case 1:
    {
        struct_1 *name = NULL;
        ...
    }
    break;
case 2:
    {
        struct_2 *name = NULL;
        ...
    }
    break;
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • But does the scope of the variable be limited within this case label? – Sheldor Jul 29 '13 at 13:11
  • Yes. Scope will be limited to each case if variable declared in `case`. – Shumail Jul 29 '13 at 13:13
  • @Sheldor If you use braces like in my answer, then yes because then it will be a new scoped block. Otherwise it will not be. The `switch` statement (complete with cases) is a *single* scope, you have to introduce extra scope yourself. – Some programmer dude Jul 29 '13 at 13:13
  • @JoachimPileborg Is there a way I can use the same variable but use different structure declarations? – Sheldor Jul 29 '13 at 13:23
  • @Sheldor My answer was just updated (Thanks Eric), please see the modifications. – Some programmer dude Jul 29 '13 at 13:29
  • @Sheldor: Yes. I added a second case showing that you can have another declaration of `name` with a different type. Each set of braces, `{…}`, creates a different *block* in C, and each block has a separate scope, so the names in it are separate from names in other blocks (except that blocks can be nested inside other blocks, in which case the inner blocks can see the names of outer blocks, unless those names are hidden by more declarations in inner blocks). – Eric Postpischil Jul 29 '13 at 13:29
  • @JoachimPileborg Thanks...but is there a way I can continue using the variable "name" outside switch? Basically I'm trying to do code reduction where the structure contents are almost same but separate function is written for each of these structures. I'm trying to club them into a single function. – Sheldor Jul 29 '13 at 13:34
  • @Sheldor Ah, then no there is not. You might be able to solve it with a `union` variable declared before the `switch` but then you need to know which field to use after the `switch`. – Some programmer dude Jul 29 '13 at 13:35
  • 1
    @Sheldor: When compiling code that uses `name`, the compiler must know the type of `name`, because the code depends on the type. E.g., if the code contains `name.foo * 3`, the compiler needs to know where `foo` is in `name` and whether it is `int` or `float` or something else, because different instructions are generated depending on those things. So the C language requires `name` to have one completely specified type at each point in the code where the object it refers to is used. So, after the `switch`, `name` must have one known type. – Eric Postpischil Jul 29 '13 at 13:38
  • Thanks @EricPostpischil for the clarification. I was also of the same understanding but just trying to see if there's a way out :) – Sheldor Jul 29 '13 at 13:47
0

Declare variables before switch

Shumail
  • 3,103
  • 4
  • 28
  • 35
0

If I understand that you want to keep same variable "name" and use it later on as pointer to structure selected by case. Then you can use void *name (declare before switch), and in each case type-cast this pointer as per the desired structure pointer. And use "name" later on as and when required.

Kavan Shah
  • 11
  • 1
0

Union

To be able to use the same variable name for different types, you could also use a union of the different types and declare the variable of that union type before the switch statement.

Example code:

typedef union {
    struct_1 s1;
    struct_2 s2;
    struct_3 s3;
} anyof123;

and before the switch statement:

anyof123 *name = malloc(sizeof(anyof123)); // Example, allocate some memory.

and in the switch statement:

case 1:
    {
        struct_1 s1 = ...; // Just an example, fill with your own declaration.
        name->s1 = s1;
    }
    break;

and the resulting variable name can be accessed outside the switch scope afterwards.

Void pointer

As pointed out by Kavan Shah, you could use a void pointer and remember to cast it to the wanted type each time you access it, like this:

((struct_1*)name)->my_data = ...;
miikkas
  • 818
  • 1
  • 8
  • 25
0

void pointers can be dereferenced, otherwise they would be mostly useless. But the compiler needs to know what data type the pointer is pointing to, so you have to use a typecast whenever you want to acces the data the pointer is pointing to.

miikkas answer above already shows an example of how the typecast looks like.

K.Isbruch
  • 145
  • 2
  • 8