Question about pointers and arrays

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;
}

Results

Address
009EF96C

Contents
13970087

Address
009EF970

Contents
10418564

Address
009EF974

Contents
268401582

Address
009EF978

Contents
268680072

Address
009EF97C

Contents
268680072

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).

Thank you for your time!

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…

That would be the same thing in this case. The array was created on the stack and would be destroyed at the end of the scope.

You could either make the array a global variable, or part of a class either static or non static.

1 Like

I have the arrays like this:

class Security
{
public:
	Security(); // constructor

	int Get_Arm_Pass(int passtype) const;
	int Set_Pass(int passtype, int Input[], int size);

private:
	int armpass[4] = { 1, 2, 3, 4 };
	int autopass[5] = { 1, 2, 3, 4, 5 };
	int manualpass[6] = { 1, 1, 1, 1, 1, 1 };
};

They’re obviously non-static. I’ll run some tests and I’ll let you know tomorrow.
Thanks Dan once again for the help.

Yep, it works:

class Get_Set_Pass
{
public:
	Get_Set_Pass(); // constructor

	int const * Get_Pass() const;

private:

	int armpass[4] = { 1,2,3,4 };
};

Method:

int const * Get_Set_Pass::Get_Pass() const
{
	int const * p = armpass;
	return p;
}

Output:

1
2
3
4

Thanks again Dan

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.

#include <array>
class Security
{
public:
	Security(); // constructor
private:
	std::array<int, 4> armpass = { 1, 2, 3, 4 };
	std::array<int, 5> autopass = { 1, 2, 3, 4, 5 };
	std::array<int, 6> manualpass = { 1, 1, 1, 1, 1, 1 };
};
1 Like

Didn’t think of that at all. Thanks for the tip!

Privacy & Terms