9

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
  • 22,383
  • 32
  • 112
  • 130
Iancovici
  • 5,574
  • 7
  • 39
  • 57
  • 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

33

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
  • 223,364
  • 34
  • 402
  • 480
  • 3
    He states C in the subject, but tagged both C and C++. This answer works well for one of those. :) – Joe Jun 10 '13 at 14:12
  • and for C he gave the only way to make an alias. Otherwise he can make pointers... but they're not aliases. – zmo Jun 10 '13 at 14:12
  • 2
    You meant `int* j = &i;` didn't you? Because your code doesn't make any sense. – Aseem Bansal Jun 10 '13 at 14:14
  • @juanchopanza The earlier `int& j = j;` wasn't making any sense. No relation of j to i. – Aseem Bansal Jun 10 '13 at 14:19
  • @Zel Right, that was a silly typo. Sorry about the confusion. – juanchopanza Jun 10 '13 at 14:20
  • @juanchopanza Let me know how I can fix the question so it's more appropriate. I can't ask new questions because of this negative feedback – Iancovici Jun 12 '13 at 14:24
  • Hmm... I tried `int i = 42; int& j = i; printf("j %d\n", j);` in `main()` in https://replit.com/languages/c, and it does not work - compilations fails with `error: expected identifier or '('`, with a caret `^` pointing at the ampersand `&` in `int& j = i;`. Has anyone tried to compile this for C language - and in what compiler does it work? – sdbbs Oct 20 '22 at 03:14
  • @sdbbs C doesn't have references as explained in this [answer](https://stackoverflow.com/a/4305686/7075416). References are valid in C++. In the case of C, you could use pointers instead of references. – Harish Ganesan Nov 04 '22 at 11:25
13

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
  • 269
  • 2
  • 5
  • 2
    This should be the accepted answer. Works in C, b is const pointer. – j b Dec 03 '18 at 11:05
  • How would this work for arrays? Say, `uint8_t test[2] = {0x10, 0x20}; uint8_t* const lt = test; printf("lt[1] %X", lt[1]);` compiles and works, but I'm not sure if space for pointer is reserved (also, note that the `const` just prevents reassigning e.g. `lt = NULL;` later - but it does not prevent changing elements via `lt[1] = 0x05;`). – sdbbs Oct 20 '22 at 03:26
9

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
  • 647,829
  • 179
  • 1,238
  • 2,067
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,511
  • 2
  • 28
  • 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,387
  • 2
  • 16
  • 24