-1

so I was presented with this code and I get most of it up until it's supposed to assign a pointer to an array. The object is to assignment is to have to program print out 100 99 98 3 2 1. So it seemed to me like function f does nothing so I deleted it. My question is how do I assign the pointer to the array within the function? I also thought returning the array would work but that didn't work out. Thanks in advance

#include <iostream>
using namespace std;

int* nochange(int* p)
{
    return p;
}

int* getPtrToArray(int& m)
{
    int anArray[100];
    for (int j = 0; j < 100; j++)
        anArray[j] = 100-j;
    m = 100;
    return nochange(anArray);
}

void f()
{
    int junk[100];
    for (int k = 0; k < 100; k++)
        junk[k] = 123400000 + k;
    junk[50]++;
}

int main()
{
    int n;
    int* ptr = getPtrToArray(n);
    f();
    for (int i = 0; i < 3; i++)
        cout << ptr[i] << ' ';
    for (int i = n-3; i < n; i++)
        cout << ptr[i] << ' ';
    cout << endl;
}c
  • 2
    You have created local int anArray[100];, the pointer become invalid once you return it from function. – Sumeet Nov 26 '18 at 07:01
  • *"so I was presented with this code"*- tell whomever presented it their code invokes undefined behavior, and even guessing what happens is pursuit of madness. `anArray` is local to `getPtrToArray`. The function `nochange` is pointless, returning the pointer it was given, in this case the pointer to the first element of `anArray`, which expires as soon as `getPtrToArray` returns. That pointer result, therefore, is worthless. Any reference to, or through, it invokes *undefined behavior*. – WhozCraig Nov 26 '18 at 07:09
  • Things to watch out for, 1) Local variables are created on the stack, if you want visibility outside of a function either use a global or dynamically allocate the memory for the array and manage it yourself or as suggested make it static to the function. 2) Pointers can be returned or passed back as a parameter but only if you pass the address of the pointer. – SPlatten Nov 26 '18 at 07:26

2 Answers2

2

You may return the pointer from function like this:

int* getPtrToArray(int& m)
{
    int *anArray = new int[100];
    for(int j = 0; j < 100; j++)
        anArray[j] = 100 - j;
    m = 100;
    return anArray;
}

int main()
{
    int n = 0;
    int* ptr = getPtrToArray(n);

    for(int i = 0; i < 3; i++)
        cout << ptr[i] << ' ';
    for(int i = n - 3; i < n; i++)
        cout << ptr[i] << ' ';
    cout << endl;

    delete []ptr; // you should free memory here
}    

But it's a bad style: allocate memory in one place, free up in another.

Better transfer pointer to array into the function, like this:

void fillArray(int *arr, int m)
{
    for(int j = 0; j < m; j++)
        arr[j] = m - j;
}

int main()
{
    const int M=100;
    int arr[M] = {0};
    fillArray(arr, M);
    //...
}

A more modern approach involves the using of vectors:

#include <vector>

vector<int> getArray()
{
    vector<int> arr(100);
    for(int j = 0; j < 100; j++)
        arr[j] = 100 - j;
    return arr;
}

int main()
{
    auto arr = getArray();
    size_t n = arr.size();
    // ...    
    for(int i = n - 3; i < n; i++)
        cout << arr[i] << ' ';
}    
snake_style
  • 1,139
  • 7
  • 16
  • thanks it worked out great, I could've sworn I had tried having the function return the array but perhaps there was something else up. Is there a point assigning 100 to m? and also why was a new array necessary? Thanks in advance – Sero the Hero Nov 26 '18 at 07:27
  • @SerotheHero If you use vectors, the size is storing inside it. You can get it by _size()_ function. In your _getPtrToArray_ func, you returned a pointer to a local array, which is destroyed after exiting the function, so you need "new" to create array in heap, not at stack. – snake_style Nov 26 '18 at 07:31
0

anArray is local to the function getPtrToArray, hence the lifetime and visibility is only to the function getPtrToArray. Defining it as a static array would increase it's lifetime to program scope.

static int anArray[100];