-2

I have to use same variable name with different user define datatypes.

Eg.

if (scan = NEAR)
{
  Near_t p_Data = getData()->Near;
}
else if (scan == FAR)
{
  Far_t p_Data = getData()->Far;
}

Data_1 = p_Data->d1;
Data_2 = p_Data->d2;

Thus I want to eliminate the repetitive code for different datatypes. In the above manner, it throws error since declaration is within the scope of conditional statement & also redefinition is happened How to achieve this functionality otherwise?

Meenu
  • 1
  • 2

3 Answers3

1

This is absolutely not possible. There are two ways to avoid repetition in this situation:

  1. Reorganize your data so that there is one data type instead of a bunch of different structures with similarly-named fields.
  2. Stuff repeating code in a macro.
n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
0

Your question is not complete, what exactly are you trying to accomplish?

For problems like this you should use union. For example:

union Some_union {
   int i;
   float f;
} data;

The memory occupied by a union will be large enough to hold the largest member of the union. Only one member of union can be accessed at a time.

Read more about unions here: Examples of Union in C

Community
  • 1
  • 1
Aleksandar Makragić
  • 1,957
  • 17
  • 32
  • How, given your opening sentence (which is entirely accurate) can the follow-up directed instruction "For problems like this you should use..." be considered anything *besides* a wag? – WhozCraig Jun 10 '16 at 06:59
  • "Declaring and initializing same variable but with different datatypes based on conditional statements in C" – Aleksandar Makragić Jun 10 '16 at 07:00
  • Maybe this will make what I eluded to clearer: Relating it to *the OP's posted code* would probably be more helpful, not only for the OP, but anyone else that reads the answer. – WhozCraig Jun 10 '16 at 07:03
0

Simply use common sense when designing the program:

Type_d1 d1;
Type_d2 d2;

if (scan == NEAR)
{
  d1 = getData()->Near->d1;
  d2 = getData()->Near->d2;
}
else if (scan == FAR)
{
  d1 = getData()->Far->d1;
  d2 = getData()->Far->d2;
}

Data_1 = d1;
Data_2 = d2;
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • Copy&Paste typo: `if (scan = NEAR)` --> `if (scan == NEAR)` – LPs Jun 10 '16 at 08:27
  • @LPs Oh yeah. Indeed it's not a good idea to write bugs. Don't do that. This particular bug would be caught by any half-decent compiler though. – Lundin Jun 10 '16 at 08:33
  • Here 'Near' and 'Far' are bigger structures. I need to access more attributes not only d1 and d2 (d1, d2, ....d12). In that case, the code length also increases. My intention was to avoid this. – Meenu Jun 10 '16 at 08:50
  • @ManeeshaKrishnan Tough luck then, you should have thought of this when you designed the structs, their design seems to be the root of the problem. Sounds very fishy that you have members named d1 to d12, why not use arrays? If you are stuck with the structs now, perhaps you can make a common accessor function for each struct, which returns a copy of the shared members of each struct. – Lundin Jun 10 '16 at 08:54