![]() |
|
#1
|
|||
|
|||
Alter Reference to ArrayHello to all expect C++ programmer, i have a reference to an array but i want sort it using that reference.
How do i accomplish this ? CPP / C++ / C Code:
This can accomplish with pointer notation and array notation. I know both of that but i want try some new techniques. Thanks for your help. |
|||
|
#2
|
|||
|
|||
Re: Alter Reference to ArrayQuote:
It is possible to have a reference variable that is a reference to an array, but its value must be assigned at the time it is declared. This makes it particularly unsuitable for use in a general-purpose sort routine. If the sort routine itself has a parameter that is a reference to an array, it must be declared as a reference to particular sized array. Before getting wrapped up into some sort of dead-end sorting project, you might consider something like: CPP / C++ / C Code:
Output Code:
Note that using a reference to an array means that the function can only be used with arguments that are arrays of that exact same (fixed) size. Not very useful for a general-purpose sort routine, I'm thinking. The function that uses a pointer can be used with arrays of any size. (And also can be used in C programs as well as C++.) Note that the code inside both functions uses the same notation to access array elements. Regards, Dave |
|
#3
|
|||
|
|||
Re: Alter Reference to ArrayThanks for your explanation.
In conclusion, pointer is the most powerful tool in Memory Address Register or Memory Data Register. I would like to highlight one thing to all C++ programmer, array of references is illegal in VS 2005. CPP / C++ / C Code:
Quote:
|
|
#4
|
|||
|
|||
Re: Alter Reference to Arrayhi
i am not getting what exactly you trying to state.i tried compiling your code by making following changes,it compiled successfully: CPP / C++ / C Code:
1. i have read in reown book that array is reference is not possible.Guess is not right. 2. Dyamic allocation of reference or array of references is possible?? Last edited by admin : 08-Jun-2008 at 21:28.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|
#5
|
|||
|
|||
Re: Alter Reference to ArrayQuote:
1. Reference variables. 2. Passing arguments to functions by reference. In any case when you are writing a function that has to handle different size arrays, reference variables or pass-by-reference function arguments are of limited usefulness, compared with pointers. Maybe that is what you are getting at??? Quote:
Regards, Dave |
|
#6
|
||||
|
||||
Re: Alter Reference to ArrayQuote:
1. In your program, the first parameter is not an array of references. It is a reference to an array of 15 ints. The following statement in your program declares a variable to be a reference to an array if 15 ints: Quote:
The variable aReference can be thought of as another name for the variable number, which is an array of 15 ints. Therefore that variable can be used as the first argument when calling your function. Nothing about it is illegal. In the Original Poster's example of what wouldn't work, the following attempts to declare an array of references to int: CPP / C++ / C Code:
2. If you go to the trouble to implement the function with that argument given in your program (a reference to an array of 15 ints), it can only be used to sort arrays of 15 ints. That might be useful to you, and I invite you to complete the program. I am not personally interested in pursuing it. (And I can't think of a "good" reason to do so for this particular kind of function. Why would I want to create a sorting function that can only sort one size array?) 3. If you can think of a way to use an array of references, then you might want to contact the international standards committee and suggest that they add it to the next release of the C++ language standard. Note, please that I am not suggesting that there would never be a possible use for such things. But the fact is, that it is not possible to have an array of references according to the current Standard. Quote:
Section 8.3.2, paragraph 4: "There shall be no references to references, no arrays of references, and no pointers to references." Quote:
In paragraph 3 of Section 8.3.2: "It is unspecified whether or not a reference requires storage" You see, since a reference can be thought of as a name of another variable, it is not possible to allocate a reference, it simply wouldn't make sense to allocate storage for a reference. (What the compiler does internally with references is up to the compiler writer. What the user does is use it as a name for another variable.) Regards, Dave |
|
#7
|
|||
|
|||
Re: Alter Reference to ArrayThanks Dave!!
|
Recent GIDBlog
Problems with the Navy (Chiefs) by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| where is the problem and can you fix it (php) | oggie | MySQL / PHP Forum | 8 | 14-Apr-2008 16:08 |
| Getting a line error in register | oggie | MySQL / PHP Forum | 5 | 13-Apr-2008 17:16 |
| Two-Tier data dissemination code installation problem | nidhibansal1984 | Computer Software Forum - Linux | 6 | 16-Sep-2007 11:13 |
| gnu.linkonce undefined reference | newbie06 | C++ Forum | 4 | 13-Mar-2007 10:53 |
| Need help deleting the last element in the array | headphone69 | C++ Forum | 2 | 15-Mar-2006 20:31 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The