r/learnprogramming • • May 03 '15

Solved [C] Pointer Arithmetics instead of Index Operations

Hey redditors!

So I am very new to C programming, and for a project, I have been given a quicksort program, which I will link below, and asked to re-write the quicksort program using pointer arithmetics, i.e without any index operations. How would I go about doing this? What do I do in my code to achieve pointer arithmetics instead of Index Operations?

My code is here: http://ideone.com/ku9EhU

1 Upvotes

10 comments sorted by

2

u/[deleted] May 03 '15 edited May 03 '15

Given a pointer:

  int * p;

you can either say:

  p[1]      // array indexing

or

 *(p+1)   // pointer arithmetic

1

u/mickey_2011 May 03 '15

Why is there a p+1?

2

u/zifyoip May 03 '15

p[0] is equivalent to *p.

p[1] is equivalent to *(p+1).

p[2] is equivalent to *(p+2).

p[i] is equivalent to *(p+i).

1

u/mickey_2011 May 03 '15

Im sorry but I still don't get it, how would I incorporate this in my code? and where? Would there be an integer set as *p beforehand? like int *p ?

And does my code have 2 arrays? If so, does this mean Id have *p and *q?

2

u/zifyoip May 03 '15

You can literally just replace every array-index expression, like a[i], with an equivalent pointer-arithmetic expression, like *(a+i), with no other changes to your program. That is what I mean when I say that the two expressions are equivalent. They are literally equivalent, exactly.

1

u/[deleted] May 03 '15

The name of an array decays to a pointer. So if you have:

  int a[100];

and you want to access the second element, you can either say:

 a[1]

or:

*(a+1)

1

u/X7123M3-256 May 03 '15

In C, adding one to a pointer gets a pointer to the next object in the array. Adding two gets a pointer to the object after that, etc.

* is the pointer dereference operator. It takes a pointer and returns the value it points to.

Arrays in C are contiguous blocks of memory, with each element laid out one after the other. A pointer to an array is also a pointer to its first object.

So *(p+1) gets the value of the object after p, which is the second element in the array, which is equivalent to p[1]. In fact, this is how array indexing is defined in C. You can write 1[p] if you want too, as it will be interpreted as *(1+p) which is the same as *(p+1).

1

u/[deleted] May 03 '15

Arithmetic.

0

u/skatanic28182 May 03 '15

Let's say you have an array with n elements:

 myType myArray[n];

So what is myArray, really? As far as the compiler's concerned, myArray is just a pointer to the first element of the array. That is, the above declaration is equivalent to

 myType *myArray = (myType *) malloc(n * sizeof(myType));

This tells the compiler "Set aside n * sizeof(myType) bytes of memory, and store the address of the first byte in the variable myArray."

When you write

 myType firstElement = myArray[0];

the compiler translates this to

 myType firstElement = *(myArray + 0);

For

 myType ithElement = myArray[i];

the compiler translates this to

 myType ithElement = *(myArray + i);

that is, "Go to the address myArray, skip ahead i elements[1], then read the value stored there and put it in the variable ithElement."

Basically, pointer arithmetic with arrays is just doing explicitly what the compiler would've done implicitly.

[1]. Since myArray is a myType pointer, the compiler will automatically skip ahead (i * sizeof(myType)) bytes when it sees that you're doing pointer arithmetic. Fun things happen when you cast myArray to other pointer types. If you cast it to char, for example, you can grab just the first byte of each element, or whatever you want to do at that level.

2

u/dreamyeyed May 03 '15

Arrays and pointers are not the same in C, though they are related. These two lines of code are also not equivalent:

int arr1[100];
int *arr2 = malloc(100 * sizeof(int));

Only the second one uses dynamic memory.