Ben
Ben

Reputation: 2725

c++ pointer syntax error

I'm studying for an exam in c++ and i have a question on the past papers

"Write a function in C++ that takes as input an array of doubles and the length of the array, and returns an array twice the length. The first half of the returned array should contain a copy of the contents of the original array. The second half of the returned array should contain the contents of the original array in reverse order."

"The function should have the following prototype: double *copy_and_reverse(double *a, int length);"

since im obviously new to c++ i got stuck in my solution, my code so far is:

double *copy_and_reverse(double *a, int length){
    double *b[length*2];
    for(int i=0;i<length;i++){
        *b[i]=a[i];
    }
    for(int i=length;i<length*2;i++){
        int w=length-1;
        *b[i]=a[w];
        w--;
    }

    return *b;
}

int main()
{
double nums[2]={1.23,5.364};
double *pnums=nums;
*pnums=*copy_and_reverse(pnums, 2);

I think i got the core of the method correct but i'm just stuck in the syntax of using pointers, any help is appreciated and if possible a reasoning behind it so i can learn for the exam.

Upvotes: 1

Views: 400

Answers (4)

user3195397
user3195397

Reputation: 236

I assume this is not your homework and I am trying to help you out. Look at the comment of code.

    double *copy_and_reverse(double *a, int length)
    {
       double * b = new double[length*2]; //create a new array using new[]
       for(int i=0;i<length;i++){
       b[i]=a[i];                         //addressing element with []
    }

    int w=length-1;                           //I assume this is what you want
    for(int i=length;i<length*2;i++){
        b[i]=a[w];
        w--;
    }

         return b;
    }

    int main()
    { 
         double nums[2]={1.23,5.364};
         double *pnums = copy_and_reverse(nums, 2);
         delete[] pnums;
     }

Also noted the memory is allocated in the function, so in the main, you want to delete it by using [].

Upvotes: 0

c-smile
c-smile

Reputation: 27460

There are quite many errors in your code. The major one is that you need to allocate new array of doubles. And return that array. I'd suggest compare this with your version line by line:

double *copy_and_reverse(double *a, int length){
    double *result = new double[length*2];
    for(int i=0;i<length;i++) {
        result[i]=a[i];
    }
    int r = length*2;
    for(int i=0; i < length;i++){
        result[--r]=a[i];
    }
    return result;
}

And your main() shall look like:

int main()
{
  double nums[2]={1.23,5.364};
  double *pnums = copy_and_reverse(nums, 2);
...
  delete[] pnums;
}

Upvotes: 1

John Dibling
John Dibling

Reputation: 101456

Ok, there are at least two problems with this:

double *b[length*2];

The first problem is that you are declaring a local array (of pointers), which you will then try to return:

return *b;

(You're returning the wrong thing here, too, but that's another story) You can't return a pointer to a locally-allocated thing because as soon as the function returns, the locally-allocated thing will be destroyed. Instead, given that you must return a pointer to the first element of an array, you have to dynamically allocate that thing using new.

Second, you can't declare an array like this using a length which s only known at runtime. But this problem will be obviated when you use new to dynamically allocate the array.

I would normally say that you shouldn't be doing any of this at all, and just use a std::vector -- but clearly a requirement of this assignment is to use a dynamically allocated C-style array. (Which I take great issue with your professor on.)

I would also say that the prototype:

double *copy_and_reverse(double *a, int length);

doesn't declare a function which takes an array, as your professor incorrectly asserts, but a function which takes a pointer to a double. That that pointer is the first element in an array doesn't magically make a an array. In short: an array and a pointer are not the same thing.

These last two observations are just for your benefit.

Upvotes: 0

Boumbles
Boumbles

Reputation: 2523

You've got a few problems with this code.

First

double *b[length*2];

Here you're declaring an array of pointers to doubles. The array is of size length * 2, however, none of the pointers in this array are valid yet. This is probably not what you intended to do.

You want an array of doubles, of size length * 2. You can't return an array in C++ but you can return a pointer to some memory that contains an array of doubles.

Let's start by allocating enough memory for all those doubles

double *b= new double[length * 2];

In your first for loop you can treat result as an array

for(int i=0;i<length;i++){
    b[i]=a[i];
}

Here you're copying the values from a for each index i to be at the same index. I'll let you figure out how to fill in the reverse part for the second half of the array. You're on the right track, however you might want to think about doing it all in one loop ;)

Your return statement just needs to return your variable b, as it's already a double *.

return b;

An important thing to remember is that you're allocating memory in this function with new. You are responsible for deleting this when you're done with it. Also, when you allocate using new and [] you have to delete using [] as well.

delete [] b;

you can call your function just by de-referencing the first item in your array.

int main() {
    double nums[2]={1.23,5.364};
    double *pnums = copy_and_reverse(&pnums[0], 2);//don't forget to clean up pnums afterwards!

Upvotes: 1

Related Questions