Solved

Using references as an array...

Posted on 2007-11-17
4
208 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

831 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