Help with a pointer to an array

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;

void Increase_Array_Elements(int* const pArray, const int size);
void Display_Array(const int* const pArray, const int size);

int main()
{
	int size;
	cout<<"Enter the size of the array: ";
	cin>>size;
	int* pArray = new int[size];

	for ( int i = 0 ; i < size ; ++i )
	{
		pArray[i] += 1;
	}

	cout<<"\nYour array: \n";
	Display_Array(pArray, size);
	cout<<"\nIncreasing all array elements by 500\n";
	Increase_Array_Elements(pArray, size);
	cout<<"\nYour array: \n";
	Display_Array(pArray, size);

	int end;
	cin>>end;
	return 0;
}
void Increase_Array_Elements(int* const array, const int size)
{
	for ( int i = 0 ; i < size; ++i )
	{
		array[i] += 500;
	}
}

void Display_Array(const int* const array, const int size)
{
	for ( int i = 0 ; i < size ; ++i )
	{
		cout<<array[i]<<" ";
	}
}


so i'm having a pretty simple problem, how can i access the elements of the array instead of accessing its memory address?
closed account (Dy7SLyTq)
in the display array function try this:
1
2
while(array++)
    cout<< *array <<" ";


if i remember my c correctly that should work
Nah no luck, remember my display function has a constant array as a parameter.
Last edited on
closed account (Dy7SLyTq)
what did you think i was doing?
how can i access the elements of the array instead of accessing its memory address?

I do not understand what the problem is, and what you're trying to achieve.

C++ inherits a feature of C, among many, which is: arrays decay to pointers. When you use an array's name, that evaluates to the memory address of the first element.

If you don't like this behavior, you should use std::vector, or std::array, or soon enough std::dynarray instead of plain C arrays, or use a different programming language altogether.
what did you think i was doing?

Error : expression must be a modifiable value
it's cool i'll figure it out, thanks anyway :)

I do not understand what the problem is, and what you're trying to achieve.

Isn't it obvious? i'm simply trying to access the elements of a pointer to an array...
Last edited on
closed account (Dy7SLyTq)
what compiler are you using? does dereferencing count as modifying?
Microsoft Visual 2012 express edition, should i try it with a different compiler or something?
Last edited on
DTSCode wrote:
what compiler are you using? does dereferencing count as modifying?
No, but array++ does.

@Uk Marine
You are accessing the elements
1
2
3
4
for ( int i = 0 ; i < size ; ++i )
	{
		cout<<array[i]<<" ";
	}
You are accessing the elements


then how come all my array elements are displayed like this:
1
2
3
4
5
6
// suppose size is equal to 5.
-842150450 -842150450 -842150450 -842150450 -842150450

Increasing all array elements by 500
Your array:
-84219950 -84219950 -84219950 -84219950 -84219950
Because you're not initializing the contents of the array anywhere. You're just getting whatever data happens to already be at those memory locations.
then how come all my array elements are displayed like this:
that is because you don't initialize the array [with 0]
Because you're not initializing the contents of the array anywhere. You're just getting whatever data happens to already be at those memory locations.

Right so is there any way possible i can access these datas?
Right so is there any way possible i can access these datas?

You need to read what people are telling you more carefully. You're accessing the data correctly. The problem is that you're not initialising the data to sensible values.
Yeah i guess you're right, sorry for misunderstanding you, all of you. Guess i better initialize my array next time, many thanks to all.
Topic archived. No new replies allowed.