Alright, so I was watching the Unreal course and I got waaaaay past the pointers lecture, being sure that I’ve understood their use.
But then I started playing with them and more specific, tried to return an array from a function, using a pointer. Then, everything went wrong.
In the code you see below, I have a method called int * returnarr() which uses a pointer to “return” an array that I’ve declared in the method. Then I declare the pointer parr which points to the first element of arr and I return it ( I could just return arr but I wanted to do it this way now).
In main() now: I declare the pointer "p", which takes the returning value from the method returnarr().
Then, I just run through the addresses with a for loop and I try to display their contents. Here’s the code and here are the results:
Code
#include <iostream>
#include <string>
int * returnarr();
int main()
{
int * p = returnarr();
for (int i = 0; i <= 4; i++)
{
std::cout << "\nAddress" << std::endl;
std::cout << p+i << std::endl;
std::cout << "\nContents" << std::endl;
std::cout << *(p+i) << std::endl;
}
std::string z;
std::getline(std::cin, z);
}
int * returnarr()
{
int arr[5] = { 1,2,3,4,5 };
int * parr = arr;
return parr;
}
It gets the addresses right, but not the contents…I get these weird numbers for some reason.
When I declare the original array in main() and use the pointer to display the contents, it works fine. What causes this issue here?
I’m also asking because I’m writing a program for an Arduino and I have a getter that takes a private array from a header (alarm and the array is the password).
Because you declared the array inside that function and nothing is keeping it “alive” so to speak, once that function ends that array will be destroyed, the pointer you return is basically invalid.
If you just do everything within main you should get what you expect, also since [] array elements are next to each other in memory you can just increment the pointer to get the next element
int main()
{
int arr[5] = { 1,2,3,4,5 };
int* p = arr;
for (int i = 0; i < 5; p++, i++)
{
std::cout << "\nAddress" << std::endl;
std::cout << p << std::endl;
std::cout << "\nContents" << std::endl;
std::cout << *p << std::endl;
}
}
I’ll have to disagree with that. Isn’t the scope within the function and not the lifetime of the array?
The array is alive as long as the program is running. So if I have a pointer that points to the first element of the array, it will be alright since the array is still alive in run-time. Or am I wrong? Is it because it’s a non-static local variable? I’m really confused here.
Also, to quote a part of my original question (and the part that I most want answered TBH). I want to do the same thing with a header file. I want to get and set an array that is part of a header file. Is that possible? Because if I can only do this inside the same function, then I have no choice…
Also you should probably use std::array instead of [] arrays, which is basically just a wrapper class. As this would mean you wouldn’t have to deal with pointers and just use the array as an object.