I am trying do this pratice but some how the 2nd function did return.
P.S>>Since english is my first lang.

can some one explane to me this is right path that i am doing?
here is question:

Write a function that accepts an array of integers and its size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function should return a pointer to the new array
Use ONLY pointer parameters instead of arrays in both functions; use pointers (not subscripts) to move through elements of both arrays. Before calling the function, display your original array. When the function call is completed, display the new array.

Here is what i got so far

#include <iostream>

using namespace std;
int *shifted (int * , int);

int main ()
    int array_size [30];
    int size;
    int new_number;
    cout <<"What is the element of array do you want?" << endl;
    cin >> size;

    for (int index = 0; index < size; index ++)
        cin >> array_size[index];
    new_number = *shifted(array_size , size);

    cout <<"The new element is " << new_number << endl;
    return 0;

int *shifted (int *shifting_arr , int size2)
    int *new_array=new int[size2+1];
    int being = 1;
    for (int index =0 ; index <size2 ; index ++)
        new_array [being] = shifting_arr[index];
        being ++;
    delete [] new_array;
    return new_array;

3 Years
Discussion Span
Last Post by Banfa

You have 2 problems and 1 unecessary variable.

The unecessary variable is being because the one place you use it, line 30, you could simply use index+1. U think this is better because it ties everything together through the single variable and properly maintains the relationship that element n in source array is copied to element n+1 in destination array as required in the question.

The first problem is a minor one, you never set the value of element 0 in the final array to 0 as the question requires.

The second problem is the real kicker, at line 33 you delete the array you have just created, that means that at line 34 you return an invalid pointer which in main you then try an dereference. This function is designed to allocate and return a new array so it should allocate data and not deleted it. The calling function, main, should store the returned pointer and then when it has finished with the new array it should delete it.

This remebering whose job it is to delete data is really an old C anacronism and is what classes, at least partially, help to get rid of. In production code you would expect a function like this to use a vector<> or array<> and have no need to track whose job it was to delete the array.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.