alias/reference variable in C

Programming, for all ages and all languages.
Post Reply
dansmahajan
Member
Member
Posts: 62
Joined: Mon Jan 07, 2013 10:38 am

alias/reference variable in C

Post by dansmahajan »

It may be a stupid question but I'm not able to find the answer- why there is no reference variable in C like C++??

Code: Select all

Like in C++:
int a;
int& b = a;
I think answer might be some approved C standard but i'm not sure, so what do you think ??
Icee
Member
Member
Posts: 100
Joined: Wed Jan 08, 2014 8:41 am
Location: Moscow, Russia

Re: alias/reference variable in C

Post by Icee »

Because references are basically pointer sugar, and C was specifically designed to have explicit pointer operations and as little sugar as possible.
dansmahajan
Member
Member
Posts: 62
Joined: Mon Jan 07, 2013 10:38 am

Re: alias/reference variable in C

Post by dansmahajan »

@Icee: Thank you very much.
User avatar
Wajideu
Member
Member
Posts: 153
Joined: Wed Jul 30, 2014 1:05 am

Re: alias/reference variable in C

Post by Wajideu »

You could probably get away with this though

Code: Select all

int *a = ({ static int _a = 0; &_a }); 
It's part of a gcc function expression extension, so it definitely won't work in most compilers (if at all)
dansmahajan
Member
Member
Posts: 62
Joined: Mon Jan 07, 2013 10:38 am

Re: alias/reference variable in C

Post by dansmahajan »

@DaemonR: Thank you.
But this will again involve pointers(which are apparent to programmers) and will have their own memory address whereas in C++ reference variables variable and its alias share the same memory address(Correct me if I'm wrong).
User avatar
Wajideu
Member
Member
Posts: 153
Joined: Wed Jul 30, 2014 1:05 am

Re: alias/reference variable in C

Post by Wajideu »

dansmahajan wrote:@DaemonR: Thank you.
But this will again involve pointers(which are apparent to programmers) and will have their own memory address whereas in C++ reference variables variable and its alias share the same memory address(Correct me if I'm wrong).
This in C++

Code: Select all

int &a;
a += 1;
Is roughly the same as this in C:

Code: Select all

int _a, *a = &_a;
*a += 1;
It's just syntactical sugar.
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: alias/reference variable in C

Post by Brendan »

Hi,
DaemonR wrote:This in C++

Code: Select all

int &a;
a += 1;
Is roughly the same as this in C:

Code: Select all

int _a, *a = &_a;
*a += 1;
It's just syntactical sugar.
Yes; and this in C++:

Code: Select all

void foo(int &var) {
    var++;
}
Is roughly the same as this in C:

Code: Select all

void foo(int *var) {
    (*var)++;
}
Except for the fact that in C the caller would do "foo(&bar);" (which makes it obvious that bar may be modified), and in C++ the caller does "foo(bar);" where it looks like the variable is passed by value and safe from modification when it's not.

This is what makes it "syntactical sugar poured into your gas tank". ;)


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: alias/reference variable in C

Post by bluemoon »

Brendan wrote:Except for the fact that in C the caller would do "foo(&bar);" (which makes it obvious that bar may be modified), and in C++ the caller does "foo(bar);" where it looks like the variable is passed by value and safe from modification when it's not.
Agree, therefore I still pass by pointer when the value would be modified, although this means adding an extra a null check.

Pass by reference is still useful when it's const:

Code: Select all

int foo(const MyClass& c) {
    return c.get_foo();
}
Post Reply