0

I'm new in C++ and trying to learn C++ by myself. I wrote codes for my project but I'm experiencing difficulties returning an array from an accessor of a class.

I have a class called LoadData which accept array_a[]. LoadData class constructor copies memory block of array_a to arrayA. An accessor, getArray() attempt to return a pointer to arrayA.

In main(), I create an array of objects, objVec[obj_i], which collects data and store in the class. When I try to return an array from LoadData and print out the contents, I get only the first element of the array correctly and the rest seems to be returning the memory addresses.

I believe that the problem I'm having is either in the way I'm returning an array in accessor or the way I'm calling getArray(). I also have lot of trouble using pointers.

I'm struggling with this for about a week so any suggestions and inputs would be appreciated! Thank you very much in advance.


class LoadData{

private:
  float* arrayA;

public:
//constructor
  LoadData(float array_a[])

//accessor
  float getArray(){return* arrayA}

};

LoadData::LoadData(float array_a[]){
  arrayA = new float[sizeOfArray]
  memcpy(arrayA, array_a, sizeof(int)*sizeOfArray);
}

int main(){

class LoadData;
int objSize = 6;
LoadData **objVec = new LoadData*[objSize];

obj_i = 0;

//loop to load data
while (...){

objVec[obj_i] = new LoadData(array_a)

}

float *copyArray;
*copyArray = objVec[1]->getArray();
for (int i = 0; i<sizeOfArray; i++){
cout<<*(copyArray + i) <<'\t';
}

//end of main
}
2
  • 1
    I'm new in C++ and trying to learn C++ by myself Then may I suggest you drop this and use std::vector instead. What seems simple to you in your current example will become a headache as soon as you start to write anything other than a toy program. Commented Nov 4, 2014 at 23:12
  • I think you are right. I'll use std::vector when I start coding other things; array gives me so much issues. Thanks for your comment! Commented Nov 5, 2014 at 13:41

2 Answers 2

1

Unfortunately I am unable to just add to molbdnilo's answer. But the short answer is he is correct you are returning the float value of the first index of the array. Which is not the same as the pointer to the first index of the array. I did write the following code which I verified shows what he was explaining. Simply compile it, bear in mind in is just outputting to the console to show the values. I hope that example shows what he and I are speaking about.

/***********************
** Include Directives **
***********************/
#include <iostream>
using namespace std;

/**************************
** Function Declarations **
**************************/
float getArray(float* myArray);
float* getArrayPtr(float* myArray);

int main()
{
    // MyVar stores return of type float value of array index [0]
    float myVar = 0.0f;
    // MyVar2 stores return of type float* value of memory address location
    float* myVar2 = 0;

    float myArray[10] = {1.0f, 2.0f, 3.0f, 4.0f, 5.0f,
                        6.0f, 7.0f, 8.0f, 9.0f, 10.0f};

    myVar = getArray(myArray);
    myVar2 = getArrayPtr(myArray);

    cout << endl;

    // This value will be the first array index value
    cout << "My Var = " << myVar << endl;

    // This value will be the address of the first element of the array
    cout << "My Var Ptr = " << myVar2 << endl;

    return 0;
}

float getArray(float* myArray)
{
    for(int counter = 0; counter < 10; ++counter)
    {
        cout << myArray[counter] << endl;
    }

    return* myArray;
}

float* getArrayPtr(float* myArray)
{
    return myArray;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for taking your time writing codes to support your's point. I run your codes and it helped me to understand more about pointer (thought it is still hard for me!!). I really appreciate your kindness. I modified my codes and it is now working!! at least till I encounter some other issues :)
It seems that I cannot vote two people on the same question post. I will vote for molbdnilo since he gave me an answer first. I'm sorry. You comments clarified a lot of things. Thanks again!!
0

The signature of

float getArray(){return* arrayA;}

says that you're returning a float, and you are.

If you want to return the pointer (which is not the same thing as an array):

float* getArray(){ return arrayA; }

1 Comment

Thanks a lot for your comment. You suggestion helped me solving my problem. I really appreciate your input!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.