Solved

Using references as an array...

Posted on 2007-11-17
4
198 Views
Last Modified: 2010-04-01
Hi,

void myFunction(char &myArray, unsigned int arraySize){
     for(unsigned int i=0;i<arraySize;i++)
          cout<<myArray[i];
     cout<<endl;
}

Produces a "subscript requires array or pointer type"? I'd prefer to stick with references if possible, and I'm going to be passing in things like:

char *myArray=new char[20];
myFunction(myArray[10], 2);

so will I still be able to use references in this manner?

Thanks,
Uni
0
Comment
Question by:Unimatrix_001
  • 2
  • 2
4 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 20304340
I'm confused at what you're trying to do :

        char *myArray=new char[20];

creates an array of 20 chars.

        myFunction(myArray[10], 2);

passes the 11-th character from that array.

        void myFunction(char &myArray, unsigned int arraySize){

accepts that 11-th character by reference.

        cout<<myArray[i];

but then you try to treat the character as an array ...
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20304345
Did you mean this :


void myFunction(char *myArray, unsigned int arraySize){

     for (unsigned int i = 0; i < arraySize; i++)

          cout << myArray[i];

     cout << endl;

}
 

char *myArray = new char[20];

myFunction(&(myArray[10]), 2);

Open in new window

0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 20304348
Actually... yep, that's it... I'm getting a little confused with how references are used... My bad.
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 20304350
Certainly did. ;)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Grammars for C C++ and java 1 114
Printing the elements of a set declared inside a map in C++. 3 38
C++ mouse_event mouse look 7 69
VS2015 Redefinition errors 4 27
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now