5

I want to use the same variable name with a different datatype in C program without casting.

I really wanna do that don't ask why.

So how can I do that ?
And how can I handle the error if this variable doesn't exist while doing prophylactic unsetting ?

AssemblerGuy
  • 603
  • 2
  • 6
  • 12
  • 6
    I'm not seeing why you would do this. I suspect you are trying to achieve something that is better done some other way. Why don't you tell us what you're trying to do so that we can solve your actual problem instead? – Anon. Dec 06 '10 at 23:19
  • 5
    "Don't ask why" is the wrong thing to say. The only way to answer a question like this is to ask why. – David Heffernan Dec 06 '10 at 23:23
  • @Hans Passant: I didn't get the joke. – AssemblerGuy Dec 06 '10 at 23:38

5 Answers5

15

You can't. The closest you can get is creating separate scopes and using the same variable name in them:

 {
     int val;

     // do something with 'val'
 }

 {
     double val;

     // do something with 'val'
 }
Justin Spahr-Summers
  • 16,893
  • 2
  • 61
  • 79
2

If you want the same memory to be referenced with two different types, use a union. Otherwise, know that what follows is a terrible idea.

int foo;
float bar;
#define MY_NAME foo
// use MY_NAME as an int.
#undef MY_NAME
#define MY_NAME bar
// use MY_NAME as a float.
nmichaels
  • 49,466
  • 12
  • 107
  • 135
1

I don't believe this is possible in C. The only way I can imagine doing this would be to write your program such that the two different variables exist in completely different scopes. Such as when you use them in different functions. Other than that, you're stuck with your first variable, pick a different name.

My suggestion -- if you absolutely require then to exist in the same scope -- would be to prefix the name with a type identifying letter, so:

int iVal;
double dVal;
Daniel Bingham
  • 12,414
  • 18
  • 67
  • 93
  • lol, or you could append a postfix or change a different name. It's a solution. *shrug* With out knowing more about the context, can't suggest a better naming scheme, so that's the best generalized one for this situation ;) – Daniel Bingham Dec 06 '10 at 23:18
  • Agreed. For what it's worth, I belong to the camp of naming your variables according to their actual purpose. So the problem of having two variables of two different types and wanting to call them both `val` doesn't occur too often for me :) – Stuart Golodetz Dec 06 '10 at 23:20
0

When you define a variable with a name that already exists, the new definition "hides" the old one.

#include <stdio.h>
int main(void) {
    int variable = 42;
    printf("variable is an int and its value is %d\n", variable);
    {
        double variable = -0.000000000000003;
        printf("variable is a double and its value is %g\n", variable);
    }
    {
        FILE *variable = NULL;
        printf("variable is a FILE * and its value is NULL :-)\n");
    }
    printf("variable is an int again and its value is, again, %d\n", variable);
    return 0;
}
pmg
  • 106,608
  • 13
  • 126
  • 198
  • What you're really doing here is using curly braces to create different scopes in which `variable` exists as 3 separate variables. @Justin Spahr-Summers already suggested this, and - at least IMO - this doesn't really seem to be what the OP asked. – GreenMatt Dec 06 '10 at 23:27
  • I agree @GreenMat. This is just another example for OP. – pmg Dec 06 '10 at 23:31
0

You can't change the type of a variable in C. You can use a little preprocessor trickery to get the illusion of what you want. i.e.

int myvar = 10;

// code using myvar as an int

#define myvar _myvar

char *myvar = "...";

// code using myvar as a char*

That being said, I cannot discourage this strongly enough. Don't do it!

Ferruccio
  • 98,941
  • 38
  • 226
  • 299