[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Using references as an array...

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
Unimatrix_001
Asked:
Unimatrix_001
  • 2
  • 2
1 Solution
 
Infinity08Commented:
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
 
Infinity08Commented:
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
 
Unimatrix_001Author Commented:
Actually... yep, that's it... I'm getting a little confused with how references are used... My bad.
0
 
Unimatrix_001Author Commented:
Certainly did. ;)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now