0

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;

}
3
  • 2
    re. code style, I recommend always using {} after the for to make the loop body obvious. And at the very least, you should indent the printf and place it on the following line after the for. Commented Sep 20, 2022 at 23:46
  • 1
    Next steps to learn: remove both occurrences of 10 from the code and figure out how to make the compiler count the elements for you. Commented Sep 20, 2022 at 23:47
  • 1
    #1: array is equivalent to the address of the first element: &array[0] #2: array+i is equivalent to the address of the i-th element: &array[i] #3: *(array+i) is equivalent to the value at the i-th element: array[i] Commented Sep 20, 2022 at 23:59

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

0

*(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.

3 Comments

Mostly useless trivia: due to the equivalence of *(array + i) and array[i], and commutativity of addition, they are also equivalent to *(i + array) and i[array].
thank you!! so does that mean that for example instead of doing : printf("first element of this array is : %d\n",array[0])) i can just do : printf("first element of this array is : %d\n",*(array)); ( since *(array) is the same as array[0] )
@vivi2003 Yes, *array and array[0] refer to the same element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.