Edit: My apologies, I completely misunderstood the question. I should not respond to StackOverflow in front of my coffee.
If you want to return an array or pointer, then there are two routes.
One route: new
int* n = new int[3]; n[0] = 0;
Since n is now a cumulus, you can delete it later, if you do not delete it, it will eventually cause a memory leak.
Now, route two is a slightly simpler method that I find, but it is more risky. Here you pass the array and copy the values.
void copyIDs(int arr[3] ) { arr[0] = workerID; }
Now your array is full and there is no heap allocation, so no problem.
Edit: returning a local variable as an address is bad. Why?
Given the function:
int* foo() { int x = 5; return &x; // Returns the address (in memory) of x } // At this point, however, x is popped off the stack, so its address is undefined // (Garbage) // So here our code calling it int *x = foo(); // points to the garbage memory, might still contain the values we need // But what if I go ahead and do this? int bar[100]; // Pushed onto the stack bool flag = true; // Pushed onto the stack std::cout << *x << '\n'; // Is this guaranteed to be the value we expect?
All in all, this is too risky. Do not do this.
source share