Solved

Using references as an array...

Posted on 2007-11-17
4
225 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

728 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