Help with a pointer to an array
Oct 28, 2013 at 8:53pm UTC
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?
Oct 28, 2013 at 9:04pm UTC
in the display array function try this:
1 2
while (array++)
cout<< *array <<" " ;
if i remember my c correctly that should work
Oct 28, 2013 at 9:13pm UTC
Nah no luck, remember my display function has a constant array as a parameter.
Last edited on Oct 28, 2013 at 9:13pm UTC
Oct 28, 2013 at 9:17pm UTC
what did you think i was doing?
Oct 28, 2013 at 9:28pm UTC
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.
Oct 28, 2013 at 9:38pm UTC
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 Oct 29, 2013 at 1:23pm UTC
Oct 28, 2013 at 9:42pm UTC
what compiler are you using? does dereferencing count as modifying?
Oct 28, 2013 at 9:44pm UTC
Microsoft Visual 2012 express edition, should i try it with a different compiler or something?
Last edited on Oct 29, 2013 at 1:23pm UTC
Oct 29, 2013 at 12:28am UTC
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]<<" " ;
}
Oct 29, 2013 at 1:19pm UTC
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
Oct 29, 2013 at 1:54pm UTC
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.
Oct 29, 2013 at 1:59pm UTC
then how come all my array elements are displayed like this:
that is because you don't initialize the array [with 0]
Oct 29, 2013 at 2:47pm UTC
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?
Oct 29, 2013 at 2:49pm UTC
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.
Oct 29, 2013 at 3:51pm UTC
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.