-5

When printing an array, initializing an integer works.

int a[MAX_SIZE] = {1,2,3,4,5,6,7,8,9,10};

for (int i = 0; i <= (MAX_SIZE - 1); i++)
{
    printf("%3d",a[i]);
}

However, I wonder why initializing a pointer to an integer ("walker") won't work:

int a[MAX_SIZE] = {1,2,3,4,5,6,7,8,9,10};

for (int *aWalk = a, int *aEnd = a + MAX_SIZE - 1; aWalk <= aEnd; aWalk++)
{
    printf("%3d", *aWalk);
}
Deanie
  • 2,316
  • 2
  • 19
  • 35
mintbox
  • 167
  • 1
  • 2
  • 14

4 Answers4

7

The statement int *aWalk = a, int *aEnd = a + MAX_SIZE - 1; wouldn't even work on its own, so it can't work in a loop header either. The syntax you are looking for is this:

int *ptr1 = some_address, *ptr2 = some_other_address;

This works inside and outside of a loop. Also, note that your problem is not declaring one pointer but two. That's also why you are supposed to first extract a minimal example.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
1

The for initial expression can be a definition for multiple variables as long as it is combined as a single definition:

int a[MAX_SIZE] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

for (int *aWalk = a, *aEnd = a + MAX_SIZE; aWalk < aEnd; aWalk++) {
    printf("%3d ", *aWalk);
}

Note that it is more generic to define the end pointer to point past the end of the array as this form can handle slices of width 0.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

You can do it this way only using pointer arithmetic. This is working because you are declaring an array of data and this data is continuously stored in the memory.

int a[] = {1,2,3,4,5,6,7,8,9,10};

for (int *aWalk = a; aWalk < (a + sizeof(a) / sizeof(int)); aWalk++)
{
    printf("%3d", *aWalk);
}

Also try it here IDE One[^].

edit:

due to the comments I changed the code using an end pointer. code[^].

for (int *aWalk = a, *aEnd = (a + sizeof(a) / sizeof(int)); aWalk < aEnd; aWalk++)
{/*...*/}
Frodo
  • 749
  • 11
  • 23
  • I think the idea behind `aEnd` is to not evaluate the array size in every loop iteration (of course, in this case there's a good chance the compiler will optimize the calculation out. – juanchopanza Mar 21 '16 at 08:32
  • 1
    @juanchopanza - Potentially incorrect. This does not necessarily evaluate the array size in every loop iteration. `sizeof` is a compile-time operator, so `sizeof(a)/sizeof(int)` is a compile time constant. Since `a` does not change in the loop, a compiler might hoist the evaluation of `(a + sizeof(a)/sizeof(int))` out of the loop. Whether it does that or not, is a concern of how well the compiler optimises. Note, however, that this technique does not work if `a` is a pointer (which will happen if the array is passed as an argument to a function). – Peter Mar 21 '16 at 08:57
  • @Peter Which part is "potentially incorrect"? I said there's a good change the compiler will optimize the calculation out. It doesn't have to, but it can and it is likely it will. – juanchopanza Mar 21 '16 at 09:01
  • The array size is `sizeof(a)/sizeof(int)` which definitely does not need to be re-evaluated on every iteration. The end condition as a whole is more likely to be evaluated on every loop iteration than the array size. Compilers tend to be pretty good at recognising when they are dividing two compile-time constants, and less so with recognising that larger expressions involving pointers are invariant. – Peter Mar 21 '16 at 09:31
  • @Peter So what? I don't really get your point. I know all of that. The whole point is about why OP chooses to use two initializers in the loop. – juanchopanza Mar 21 '16 at 17:54
-1

You have an extra int in the second code. Also your placement of comma is wrong. Also removed unnecessary aEnd variable.

int a[MAX_SIZE] = {1,2,3,4,5,6,7,8,9,10};

for (int *aWalk = a; *aWalk < (a + MAX_SIZE - 1); aWalk++)
{
    printf("%3d", *aWalk);
}
Rishikesh Raje
  • 8,556
  • 2
  • 16
  • 31