8

I want to call the same variable with a different name, how can I assign it an alias?

Do I stick to using a macro, like

#DEFINE Variable Alias

In summary:

  1. I prefer to apply it in C
  2. I have a signal which can be more than one type of variable (temperature, distance, etc..)
  3. I want to assign more than one alias to that signal

I'm currently doing it in C using functions as the method for renaming.

So given the variable: int signal

I'd do the following

int Temperature(){return signal;}
Lance Roberts
  • 21,757
  • 30
  • 108
  • 129
Iancovici
  • 5,286
  • 6
  • 35
  • 55
  • 5
    I would see this as a definite code smell. Perhaps you can explain better what your problem is. Are these all global variables? Why would you need to alias different variables to the same thing? – Joe Jun 10 '13 at 14:11
  • 9
    Also, is it C or C++? – Joe Jun 10 '13 at 14:11
  • 2
    I can't see any good doing that kind of thing. A variable has a name related to its content, if you need to change the name in each subsection, are you sure you want it to be the same variable? – Maxime Chéramy Jun 10 '13 at 14:41
  • @Joe The variable is the status of an external signal. That external signal may be switched to poll different sensors. Preferably in C, but it can be done in C++ with extra work P.s. Trying to fix this question to be more appropriate, any opinions are valued – Iancovici Jun 12 '13 at 16:58

6 Answers6

29

The way to provide an alias for a variable in C++ is to use references. For example,

int i = 42;

int& j = i;       // j is an alias for i
const int& k = j; // k is an alias for i. You cannot modify i via k.
juanchopanza
  • 216,937
  • 30
  • 383
  • 461
8

You say

int a, *b;
b = &a; // *b is now an alias of a

I like to think of a pointer as something that gives you a variable when you star it, and the ampersand as the alias-making operator.

Eric Lippert
  • 630,995
  • 172
  • 1,214
  • 2,051
7

An alternative to Eric Lippert's answer:

int a;
int * const b = &a;

Now *b is the same as a it can not be made to point to anything else, the compiler will not generate additional code to handle a or *b, neither reserve space for a pointer.

LuisF
  • 209
  • 2
  • 4
5

C++, something along the lines of:

struct A{
    union{
        int temperature;
        int sametemperature;
    };
};
A a;
a.temperature = 2;
a.sametemperature = 3;

Depending on compiler and compatibility flags, you can get away with this in plain C. In short try leveraging anonymous unions. Question is old but, I was looking for an answer to the same question and realised there's a solution without pointers/references or macros.

Kalibr
  • 105
  • 1
  • 9
  • I like this solution, it saves the memory cost of a pointer or reference (8 bytes on 64bits architectures). – S.Clem Mar 11 '18 at 21:14
3

Why not just pass it as a parameter to the function and use the appropriate name in the function argument? I don't think #define is a good idea as it may lead to obfuscation. That is in C. Of course you can use aliases in C++.

Sid
  • 7,315
  • 2
  • 26
  • 41
1

I think that #define doesn't design for this use case. Firstly because it is a preprocessor directive which is processed before the program get compile. You lose a chance for the compiler to give meaningful warning and you might end up having a strange error or warning that is hard to fix.

Pointer seem to work but you need to use a * (dereferencing operator) every time which is dangerous, harder to read and if you miss the * you might get a segmentation fault or similar memory error.

Try post some code and we might found a proper solution that suit your need.

Nuntipat Narkthong
  • 1,357
  • 2
  • 16
  • 24