I have arranged this code in many different ways. I can't seem to make it simply print the array. I have read alot about pointers and arrays, but the explanations are scetchy at best. What exactly are the rules for properly pointing to an array?

#include <iostream>

using namespace std;

int main() {

int a[5] = {1,2,3,4,5};
int *aptr = &a[0];

for(int i = 0; i <= 5; i++)
cout << (*aptr + 1);

system("pause");
return 0;
}

int *aptr = &a[0];

When you asign a pointer to an array you don't use the & operator.

int *aptr = a;

cout << (*aptr + 1);

}

cout << *( aptr + i ) will do it. Notice the "*" outside the parenthesis, and the i to index the pointer array.

I have arranged this code in many different ways. I can't seem to make it simply print the array. I have read alot about pointers and arrays, but the explanations are scetchy at best. What exactly are the rules for properly pointing to an array?

#include <iostream>

using namespace std;

int main() {

int a[5] = {1,2,3,4,5};
int *aptr = &a[0];

for(int i = 0; i <= 5; i++)
cout << (*aptr + 1);

system("pause");
return 0;
}

It is just a little but very common fault that, you did forget to use the loops counter, i.

for(int i = 0; i < 5; i++)
    cout << (*aptr + i);

and you should ent the loop at i < 5, not i <= 5 as you start from 0

Thanks, I wasn't sure exactly, but even with that change the output is still "22222" ???

Thanks Fulyaoner! that did the trick! I was so close, so many times. It always seems to be the little things with C++.

THANKs guys,
Now it's time to read some more. If someone could write a book That you could TRULY understand on C++ = $$$$$$

Thanks, I wasn't sure exactly, but even with that change the output is still "22222" ???

I think you should delete the exe in your bin directory and rebuild.
and dont forget to make the one (1) , i.

I run your code as follows and it is fine.

#include <iostream>
using namespace std;
int main() {
 
 int a[5] = {1,2,3,4,5};
 int *aptr = &a[0];
 
 for(int i = 0; i < 5; i++)
  cout << (*aptr) + i << endl;
 
 system("pause");
 return 0;
}

output.
1
2
3
4
5


But here i must say, this code gets the 0.th value of the array, and then increases it by i's value. I mean the value seen here is not the 2th or 3th value of array, they are (1 + 0), (1+1), (1+2) , (1+ 3) , (1+4) values

If you really want to print that array, you should do the following modifications

#include <iostream>
using namespace std;
int main() {
 
 int a[5] = {11,52,3,4,5};
 int *aptr = &a[0];
 
 for(int i = 0; i < 5; i++)
  cout <<   *(aptr + i) << endl;
 
 system("pause");
 return 0;
}

or

#include <iostream>
using namespace std;
int main() {
 
 int a[5] = {11,52,3,4,5};
 int *aptr = &a[0];
 
 for(int i = 0; i < 5; i++)
  cout <<  *((&(*aptr)) + i) << endl;
 
 system("pause");
 return 0;
}

Hi there, The output is "22222" because you are not using "i" at all! cout

Thanks, I wasn't sure exactly, but even with that change the output is still "22222" ???

You are doing something different then. This code works as explained.

#include <iostream>

using namespace std;

int main() {

int a[5] = {1,2,3,4,5};
int *aptr = a;

for(int i = 0; i < 5; i++)
   cout << *(aptr + i);

system("pause");
return 0;
}

I noticed that other pleople posted the solution before I finish this post. Good that is solved

>When you asign a pointer to an array you don't use the & operator.
You don't have to. Sure, most people don't, but there's nothing stopping you from assigning a memory address from a particular element in an array like the OP did.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.