Solved

Using references as an array...

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

744 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

11 Experts available now in Live!

Get 1:1 Help Now