Click here to Skip to main content
15,400,388 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, So I understand array incrementing without pointers. However in the example below I dont understand what happens when the for loop is going for the second and third time. The value of
*i_ptr
gets incremented but where does this incremented value get stored?

// Jones, Bradley L.; Peter Aitken; Dean Miller. C Programming in One Hour a Day, Sams Teach Yourself (pp. 339-340). Pearson Education. Kindle Edition.
   /* ptr_math.c--Demonstrates using pointer arithmetic to
      access array elements with pointer notation. */

   #include <stdio.h>
   #define MAX 10

   // Declare and initialize an integer array.

   int i_array[MAX] = { 0,1,2,3,4,5,6,7,8,9 };

  // Declare a pointer to int and an int variable.

  int *i_ptr, count;

  // Declare and initialize a float array.

  float f_array[MAX] = { .0, .1, .2, .3, .4, .5, .6, .7, .8, .9 };

  // Declare a pointer to float.

  float *f_ptr;

  int main( void )
  {
      /* Initialize the pointers. */

      i_ptr = i_array;
      f_ptr = f_array;

      /* Print the array elements. */

      for (count = 0; count < MAX; count++)
          printf("%d\t%f\n", *i_ptr++, *f_ptr++);

      return 0;
  }


If the for loop in the previous code would be like this:
for (count = 0; count < MAX; count++)
          printf("%d\t%f\n", i_array[count], *f_ptr++);
It would work the same but with the difference that I would understand that count is an int that gets bigger all the time. However in the first code, there is a pointer that is incremented and I dont understand where the value is stored of the incrementations (is it maybe an int for example)?

What I have tried:

I have tried replacing a hard part of the code:
*i_ptr++
with code that I understand and also works:
i_array[count]
.
Posted
Updated 3-Aug-21 9:45am
v2

i_ptr (the pointer itself) gets incremented by the size of what it points to (in this case, the size of an int) so that it will then point to the next item.
   
Comments
Mieczyslaw1683 3-Aug-21 15:37pm
   
Hmm. Ok. So the first time in the loop its just the item 0 (in i_array). Then the second time it is item 1. Then the third time it should be item 1 again (1) because its still just *i_ptr++ according to my logic? Where does the pc add the numbers?
Greg Utas 3-Aug-21 16:12pm
   
It is incremented every time through the loop. So the third time, it points to item 2, and so on.
The variable i_ptr is a pointer to integer so it is declared a type of int *. Using the asterisk with a pointer makes it the dereference operator so it is obtaining the value at that address.

See this page for an operator precedence chart : C Operator Precedence - cppreference.com[^]. This statement there is very important :
Quote:
When parsing an expression, an operator which is listed on some row will be bound tighter (as if by parentheses) to its arguments than any operator that is listed on a row further below it. For example, the expression *p++ is parsed as *(p++), and not as (*p)++.

The expression you are asking about is actually parsed as if it looked like this :
C++
*(i_ptr++)
which means the pointer is incremented to point to the next item and then it is dereferenced. Equivalent code to that would look like this :
C++
for (count = 0; count < MAX; count++)
{
    i_ptr++;
    f_ptr++;
    printf("%d\t%f\n", *i_ptr, *f_ptr );
}
which means the output would start with the values of the second items or i_ptr[1] and f_ptr[1]. This means the code will access data beyond the bounds of the array which is a bad thing. To avoid that you could increment the pointers after displaying their values. You can do that in at least two ways. One is :
C++
for (count = 0; count < MAX; count++)
{
    printf("%d\t%f\n", *i_ptr, *f_ptr );
    i_ptr++;
    f_ptr++;
}
OR
C++
for (count = 0; count < MAX; count++)
{
    printf("%d\t%f\n", (*i_ptr)++, (*f_ptr)++ );
}
The second option deferences the pointer first and then the pointer is incremented.

Personally, I avoid writing code like this that relies on the precedence order. I prefer to make the order of operations explicit and not use compound operations, like the first option does.
   
Comments
Mieczyslaw1683 3-Aug-21 15:49pm
   
Ok. Looks like you took your time, thanks. I think that the example before the last one looks most easy for me (incrementing the pointer after displaying the values).

Now when the first increment takes place (i_ptr++;), is the array altered in a way so that it now has a number added telling about the second item in the array?
Rick York 3-Aug-21 16:30pm
   
No, the array is not altered at all in the for loop. The pointer is incremented to step through the array.
OK ... the first thing to remember is that the name of an array is a pointer to the first element - which means that an array and a pointer are interchangeable.
If fact when you use an indexer on and array, what happens is that the compiler generates a pointer:
C
int arr[10];
...
int x = arr[index++];
Generates the same code as this would:
C
int arr[10];
...
int x = *(arr + index * sizeof(int);
index += 1;
So if you want to access every element of an array in a loop, you can either access it via an indexer that you increment each time, or you use a pointer to the first element and increment that each time instead. Provided the pointer is the same type as the array elements, either approach will work fine.
   
Comments
Mieczyslaw1683 3-Aug-21 15:55pm
   
Ok. So maybe the PC has an invisible indexer for pointer-incrementation which it uses but we cant see it?
OriginalGriff 3-Aug-21 16:14pm
   
Um ... depends on the processor, they will all have an auto increment for "pointers" in the assembly code, but the size of the increment can vary.

Bear in mind that your variables often don't exist in compiled code - the processor has internal registers and local variables often get stored in them rather than in "real memory" as it's faster.

So "invisible" kinda goes with the territory! :laugh:

Basically, don't get too hung up on how something is implemented as compilers don't generally tell you and can change it in unpredictable ways depending on the optimisations it applies (which can be very different between debug and release forms for example).
As a beginner, you can give yourself too much information and that can mask what you actually need to know to use the language effectively.
Greg Utas 3-Aug-21 16:17pm
   
Sort of. If you have T* ptr (a pointer to the type T), the compiler knows how many bytes an instance of T occupies, so it generates code to add that number of bytes to ptr when it sees the expression ptr++. C++ also has an operator called sizeof that will tell you the size of a type. So, another way to write ptr++ would be ptr += sizeof(T). In this case, T is an int.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900