Home > Software engineering >  Iterating an array with pointer
Iterating an array with pointer

Time:09-21

I have trouble understanding the concept of pointers with arrays.

Why does *(array i) stand for? Is it creating a pointer? If so, how is it affecting the for loop? (I am a beginner in C)

int main() {

  int array[10] = {0, 1, 8, 2, 18, 3, 6, 2, 2, -4};

  int i;

  for (i = 0; i < 10; i  )


  printf( "array[%d] = %d\n", i, *(array i) );

  return 0;

}

CodePudding user response:

*(array i) is equivalent to array[i]. The array itself is in many ways equivalent to a pointer to its first element, and pointer arithmetic works in units of the element type. The * dereferences the pointer, i.e., the result of the expression is the value at the ith element of array.

The printf is the body of the for loop (although misleadingly separated from it by blank lines). I'm guessing the extra four * are an attempt to bold the *(array i) part, so the loop will print each element of the array: array 0 is the first element, and *(array 0) is the same as *array, i.e., the value at that position. Then array 1 is the next one (recalling that the 1 is in units of the value/element type, here int), etc.

CodePudding user response:

Adding i to a pointer value yields a pointer to the i'th object of the same type in a sequence. Assuming the declarations

char  *cp = 0x8000;
short *sp = 0x8000;
long  *lp = 0x8000;

the following is true:

       char  char *    short short *  long  long *
       ----  ------    ----- -------  ----  ------
        ---             ---            --- 
0x8000 |   | cp        |   | sp       |   | lp
        ---            |   |          |   |
0x8001 |   | cp   1    |   |          |   | 
        ---             ---           |   |
0x8002 |   | cp   2    |   | sp   1   |   |
        ---            |   |          |   |
0x8003 |   | cp   3    |   |          |   |
        ---             ---            --- 
0x8004 |   | cp   4    |   | sp   2   |   | lp   1
        ---            |   |          |   |
        ...             ...            ...

cp points to a 1-byte object, sp points to a 2-byte object, and lp points to a 4-byte object, all starting at address0x8000.

The expression cp 1 has type char * and the value 0x8001 - it yields a pointer to the first char object following the object pointed to by cp.

The expression sp 1 has type short * and the value 0x8002 - it yields a pointer to the first short object following the object pointed to by sp.

And the expression lp 1 has type long * and the value 0x8004 - it yields a pointer to the first long object following the object pointed to by lp.

This is exactly how array subscripting works - the expression a[i] is defined as *(a i) - given a pointer value specified by a, offset i elements (not bytes!) from that address and dereference the result.

Arrays are not pointers - array expressions "decay" to pointer values under most circumstances, precisely because of the semantics above. When you write something like

a[i] = some_value;

the expression a is automatically converted from type "N-element array of T" to "pointer to T" and the value of the expression is the address of the first element of a. The exceptions to this rule are:

  • when the array expression is the operand of the sizeof, _Alignof, or unary & operators;

  • when the expression is a string literal used to initialize a character array in a declaration, like char foo[] = "some string";

CodePudding user response:

The array i refers to so called "pointer arithmetics". It means that if you will point to *array value you will get the first array element (array[0]). The added numbers will refer to the next array elements. Each added value will point to the corresponding next element, like (*array 1) will refer to the value under array[1], (*array 2) will refer to the (array[2]) and so on.

Remember that the added number will be multiplied by the memory of it's type like if in this case it's int value (i.e. 4 bytes) and the (*array) address is 3000 the (*array 1) address will be 3004.

Unfortunately I don't understand and I can't describe additional stars used in *(array i).

  • Related