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

Dani AI

Generated

The thread already shows the two root causes: using the wrong expression to access elements, and an off‑by‑one / out‑of‑bounds loop. and correctly pointed out that you must index the elements (or advance the pointer) and stop at the correct bound; rightly reminded that taking the address of an element is allowed but not required. Below are clarifications and safer alternatives that are still true years later.

Pointer/array type notes to keep in mind

  • The array name a decays to a pointer to its first element (int*) in most expressions; &a is a different type (int (*)[N]) that points to the whole array. Pointer arithmetic uses the pointed type size (so p+1 advances by one int, while pa+1 for int (*pa)[5] advances by 5 ints).
  • Unary * binds so *p + i means (*p) + i (value plus i). To access the i-th element via pointer arithmetic you must use *(p + i) or index syntax p[i].
  • sizeof(a)/sizeof(a[0]) yields the element count only while a is an actual array in scope; once passed to a function it decays and you lose that information.

Safer idioms (examples)

  • Compute length at compile-time for raw arrays and use a sentinel pointer loop:

    int a[] = {1,2,3,4,5};
    size_t n = sizeof(a) / sizeof(a[0]);
    int *p = a, *end = a + n;
    while (p < end) { std::cout << *p << '\n'; ++p; }
  • Prefer modern containers for safety and clarity:

    std::array<int,5> arr = {1,2,3,4,5};
    for (int v : arr) std::cout << v << '\n';

Troubleshooting checklist

  • Check loop bounds (use < n, not <= n).
  • Verify parentheses so you aren’t adding to a value instead of indexing.
  • Clean and rebuild if the output looks stale (old exe).
  • Prefer std::array/std::vector for production code to avoid decay/size issues.

Recommended Answers

All 8 Replies

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.