3

First I declare an array a with 10 elements. Then I call the function bubbleSort bubbleSort( a, 10); where bubbleSort is a function declared as void bubbleSort(int* const array, const int size)

My question is if "array" is a pointer- which means it stored the address of array a (array= &a [0]) then how can we understand these terms array[1], array[2], array[3]... in the function bubbleSort?

It is the bubble sort program and this part is very confusing for me.

Waqar
  • 8,558
  • 4
  • 35
  • 43
nguyen tung
  • 67
  • 1
  • 6

4 Answers4

5

array[1] means, by definition in the C standard, *(array+1). So, if array is a pointer, this expression adds one element to the pointer, then uses the result to access the pointed-to object.

When a is an array, you may be used to thinking of a[0], a[1], a[2], and so on as elements of the array. But they actually go through the same process as with the pointer above, with one extra step. When the compiler sees a[1] and a is an array, the compiler first converts the array into a pointer to its first element. This is a rule in the C standard. So a[1] is actually (&a[0])[1]. Then the definition above applies: (&a[0])[1] is *(&a[0] + 1), so it means “Take the address of a[0], add one element, and access the object the result points to.”

Thus, a[1] in the calling code and array[1] in the called code have the same result, even though one starts with an array and the other uses a pointer. Both use the address of the first element of the array, add one element, and access the object at the resulting address.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
4

C defines operations of addition and subtraction of integers and pointers, collectively called pointer arithmetics. The language specification says that adding N to a pointer is equivalent to advancing the pointer by N units of memory equal to the size of an object pointed to by the pointer. For example, adding ten to an int pointer is the same as advancing it by ten sizes of int; adding ten to a double pointer is equivalent to advancing the pointer by ten sizes of double, and so on.

Next, the language defines array subscript operations in terms of pointer arithmetics: when you write array[index], the language treats it as an equivalent of *((&array[0])+index).

At this point, the language has everything necessary to pass arrays as pointers: take &array[0], pass it to the function, and let the function use array subscript operator on the pointer. The effect is the same as if the array itself has been passed, except the size of the array is no longer available. The structure of your API indirectly acknowledges that by passing the size of the array as a separate parameter.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Note that a pointer and an integer are not necessarily fully compatible with their math - usually, a pointer is unsigned, and it's size is independent of the size of an integer. – Richard J. Ross III May 06 '13 at 03:07
1

You have an array of int, identified by the address of its first element. array[1] Is equivalent to *(array + 1) which mean "The value of what is pointed by array + the size of one element, which is known as int because you prototyped it as int *"

Antzi
  • 12,831
  • 7
  • 48
  • 74
  • 1
    That's wrong. array[1] is simply *(array+1). You don't need to take into account the size of your array type. – Ryan May 06 '13 at 02:11
  • @Ryan, well, the compiler account it for you, but you are right, the code is wrong. I edit it. – Antzi May 06 '13 at 03:02
0

When you declare a to be an array of size 10, the c program stores the address of a[0] in a and since the memory is allocated continuously therefore you can access the subsequent integers by using a[2], a[4] etc. Now when you copy a to array it is actually the address that gets copied and therefore you can access the integers using array[0], array[1] etc.

Peeyush
  • 6,144
  • 5
  • 23
  • 37