# Aliases

#### mentaaal

Joined Oct 17, 2005
451
Hey guys,
I was just wandering, why is an alias created like this:

Rich (BB code):
int a = 0;
int &b = a;
Howcome this does not assign the value of a, currently 0 to the address of b?

Howcome to assign an alias it is not done like this:

Rich (BB code):
int a = 0;
int &b = &a;
This makes more sence to me anyway as the address one one gets assigned the address of the other?

Also another wee problem i am having i am trying to figure out how you can assign a string literal to a character array which is a member of a class. I tried it in the code below but it returns an error below. Could anyone show me how to do this?

Rich (BB code):
#include <iostream>
using namespace std;

int main()
{

struct test
{

char a[50];
};
test stest;
stest.a = "hello.";
return 0;
}
Rich (BB code):
1>------ Build started: Project: Test7, Configuration: Debug Win32 ------
1>Compiling...
1>Test7.cpp
1>z:\college\year2\programming\programs\test7\test7\test7.cpp(13) : error C2440: '=' : cannot convert from 'const char [7]' to 'char [50]'
1>        There is no context in which this conversion is possible
1>Build log was saved at "file://z:\College\Year2\Programming\Programs\Test7\Test7\Debug\BuildLog.htm"
1>Test7 - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

#### beenthere

Joined Apr 20, 2004
15,819
Those are compiler issues. The person/company that wrote the compiler determined the rules for how input statements are parsed and interpreted. A different language might either not have the ability to alias variables or do so using differing notation.

You may not find the function as making sense to you, but the compiler determines how you have to use it. Sometimes it can be irritating enough that you find another language to use that makes more sense (or at least doesn't irritate you every time you write code).

#### n9352527

Joined Oct 14, 2005
1,198
Alias or reference as I know it, is assigned just like normal variable assignment. So, the code:

int &a = &b;

is the correct usage.

Note that it is different in the case of pointer, which would be:

int *a = &b;

The reference to a variable is used in codes just like the variable itself. It is usually benefits the program in parameter passing. Declaring a parameter to be passed by reference:

Rich (BB code):
int add(int &a, int &b)
{
return a*b;
}

int main(void)
{
...
...
}
Note that the variable a and b are accessed by normal variable name in the function, but declared as passed by reference in the function declaration. The function is called by passing the references to num1 and num2, however some compilers will accept variable names without the reference designator as well.

Assigning a partial string, or static initial value, to a member of a union or class is possible only during initial construction. If you declare a constructor, you could pass the initial value and assigned it to the member variable. Outside of that, you could use string copy to do that or you could try assigning a full string (50 characters) and see what happens

#### mentaaal

Joined Oct 17, 2005
451
Thanks for the replies guys. Yeah i can see what you mean about the compiler determining it but it just seems so counter intuitive that i could see no logic in it at all.

And n9352527, the error I get when doing it like "int&a = &b" is:
1>z:\college\year2\programming\programs\test7\test7\test7.cpp(8) : error C2440: 'initializing' : cannot convert from 'int *__w64 ' to 'int &'

Apologies i should have specified, i am using microsoft visual 2005 c++.

String copy you say... right i will give that a shot.

Cheers

#### Mark44

Joined Nov 26, 2007
628
Alias or reference as I know it, is assigned just like normal variable assignment. So, the code:

int &a = &b;

is the correct usage.
I don't think so, but if you can prove me wrong, I'm happy to listen. Here is some code taken from Bjarne Stroustrup's The C++ Programming Language, Second Edition, in the section on references:
Rich (BB code):
int i = 1;
int& r = i; // r and i now refer to the same int
int x = r;  // x = 1
r = 2;      // i = 2
Mark

#### Mark44

Joined Nov 26, 2007
628
Hey guys,
I was just wandering, why is an alias created like this:

Rich (BB code):
int a = 0;
int &b = a;
Howcome this does not assign the value of a, currently 0 to the address of b?
At the risk of possibly saying again what someone else in this thread has said, what you're dealing with here is C++ syntax for a reference. You're looking at this from a C perspective, which doesn't apply. You cannot declare something to be of type "int &" in C.

Howcome to assign an alias it is not done like this:

Rich (BB code):
int a = 0;
int &b = &a;
This makes more sence to me anyway as the address one one gets assigned the address of the other?
References are a bit like pointers, but it's probably not helpful to think of them this way, because how they work is implementation-dependent. IOW, one compiler vendor might implement them in one way, while another is free to implement them in a different way. I'm pretty sure that the code above won't work, but I don't have the time right now to try it out.

Also another wee problem i am having i am trying to figure out how you can assign a string literal to a character array which is a member of a class. I tried it in the code below but it returns an error below. Could anyone show me how to do this?

Rich (BB code):
#include <iostream>
using namespace std;

int main()
{

struct test
{

char a[50];
};
test stest;
stest.a = "hello.";
return 0;
}
I'm coming in a bit late on this thread, but I think someone else told you how to do what you need to do here. The reason you can't do the assignment above is that what you're trying to do is assign the address of a string constant ('const char [7]') to a variable that is of type array of 50 chars ('char [50]').
Hope that helps.
Mark