Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to make a reference to an array 1

Status
Not open for further replies.

holaqtal

Programmer
May 6, 2003
7
ES
It should be quite simple, but i can't make it work:

for example:

unsigned int num[20];

and i want to make a reference the array 'num' in the function 'setter', is this how i'm supposed to do it?

void setter(const unsigned int& nn[20]){num=nn;};

I get the error: 'nn is an array of references'

thank you for your help
 
I think si better you to declare like this:
void setter(const unsigned int*& nn)

Ion Filipski
1c.bmp
 
sorry, didn't work, the compiler says:
incompatible types in assignment of `const int*' to `int[20]'
 
Any time you need to manipulate a group of identical variables e.g when you need to calculate which variable to select from the block at run time, you can use an array. An array can be created for multiple dimensions for any type of variable including user-defined types.
Whenever you calculate the address of a variable at run time , you are using a POINTER. It turns out that an array is really another type of pointer but, in fact, arrays and pointers are almost interchangeable.
While a pointer doesn't have to point to anything particular when it is created, a REFERENCE must always be initialized. An independent reference can only be initialized to an existing variable of the correct type , or to a constant.
A pointer is a variable - it can be reassigned to other addresses. A reference MUST be initialized when it is created and it can NEVER refer to a different variable.
You cannot create array of references.
Code:
void setter ( int a[])
{
}
void setter( int *pToInt)
{

}
The above function are equivalent. The compiler finds them identicall and throws an error.

void setter( int a)
{
	
}
void setter( int &refToInt)
{
}
The above functions are both accepted by the compiler but an ambigous call  is thrown depending on calling:

Example:
void main(void)
{
int b=10;
int &refToInt = b;
setter(b);
setter(refToInt); // here is ambigous 
}


void main(void)
{
int b=10;
int &refToInt = b;
setter(refToInt); // again here is ambiguos call!!
}
The solution for your problem:
void setter (int *, int dim)
{
}
or 
void setter(int a[], int dim)
{
 
}
As you can see, you have to pass the dimension of the array.
To understand more, you can read that:
void main (void)
{

   int a[10];
   int *p1ToInt = a; 
   int *p2ToInt = &a[0]; // the same as p1ToInt;
   int & refToInt = a[0]; // reference to the first element
   int & ref2ToInt = a[1]; // reference to the 2nd element
   cout<<*a<<endl; // out the a[0]
   cout<<*(a+1)<<endl; // out the a[1]
   cout<<*p1ToInt<<endl; // out a[0]
   cout<<*(p1ToInt + 1) <<endl: // out a[1]
   cout<<*p2ToInt<<endl; // out a[0]
   cout<<*(p2ToInt +1 )<<endl; // out a[1]
   cout<<refToInt<<endl; // out a[0]
   cout<<ref2ToInt<<endl;// out a[1]

   setter(a,10);
   
   
{
-obislavu-
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top