C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 12-20-2002, 06:20 AM   #1
shiv_tech_quest
Guest
 
Posts: n/a
Question How to Create reference to an array in C++

Hi Guys,

I have studied that we cannot create references to arrays in C++. But when i was reading a book by Stroustrup, he has asked in the exersice to create a reference to an array of 10 elements. Can anybody demonstrate the same??

PS: I believe i would have to create individual references to each of the element in the array..... am i right!!!!
  Reply With Quote
Old 12-20-2002, 07:11 AM   #2
Programming Sex-God
 
Polymorphic OOP's Avatar
 
Join Date: Nov 2002
Posts: 1,078
Re: How to Create reference to an array in C++

Quote:
Originally posted by shiv_tech_quest
Hi Guys,

I have studied that we cannot create references to arrays in C++. But when i was reading a book by Stroustrup, he has asked in the exersice to create a reference to an array of 10 elements. Can anybody demonstrate the same??

PS: I believe i would have to create individual references to each of the element in the array..... am i right!!!!
Whoever told you that you can't didn't know what they were talking about.

You make a reference to an array like this

Elementtype (&ReferenceName)[NumElDim0][NumElDim1][NumElDim2]; // etc. for as many dimensions as you want

For instance, if you wanted a reference to a 2 dimensional int array of dimension lengths 3 and 5 you'd do this

int Array[3][5]; // This is the actual array

int (&ArrayRef)[3][5] = Array; // Here's our reference

If your instructor told you you can't make a reference to an array but you can make a poitner to an array then he most likely didn't teach you how to make a pointer to an array -- he probably told you how to make a pointer to the first element of an array (which is very different).

A pointer to an array is done the same way as you'd do a reference to an array.

int Array[3][5]; // This is the actual array

int (*ArrayPtr)[3][5] = &Array; // Here's our pointer

Just remember that if you use a pointer to an array (as opposed to a reference) you will have to explicitly dereference it before you use it.
Polymorphic OOP is offline   Reply With Quote
Old 12-20-2002, 10:01 AM   #3
shiv_tech_quest
Guest
 
Posts: n/a
Thanks a ton Polymorphic OOP

I am enlightened by your answer.......

Have a wonderful day... and keep smiling...... you look terrific that way
regards,
shiv
  Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using Vectors. MinGW warning Viewer C++ Programming 9 03-26-2009 03:15 PM
Create Array size Question popohoma C++ Programming 3 11-04-2002 03:04 AM
Create array of CArray ooosawaddee3 C++ Programming 0 09-30-2002 11:22 AM
qt help Unregistered Linux Programming 1 04-20-2002 09:51 AM


All times are GMT -6. The time now is 01:57 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22