First of all, I'm guessing that this is not something that you wrote, but rather a problem you have to figure out (I just don't see what you would be trying to do this way =)
As to what this chunk of code does:
Code:
#include <stdio.h>
void print(int *a, int *b, int *c, int *d, int *e);
int main()
{
// creates an array of integers
int arr[] = {0, 1, 2, 3, 4,5, 6, 7, 8, 9};
// ptr gets the address of the second item in the array
int *ptr = arr + 1;
// this sends the addresses of items before and after ptr
print(++ptr, ptr--, ptr, ptr++, ++ptr);
return (0);
}
void print(int *a, int *b, int *c, int *d, int *e)
{
printf("\n%d , %d , %d , %d , %d \n", *a, *b, *c, *d, *e);
}
Now, as to what the code does. You have declared an array of ints, [0-9], and then assigned the address of the second element to ptr. The "++" and "--" either add or subtract one from that value, and based on the position of them, this is done before or after the value is passed down to the function.
When the "++" or "--" is placed BEFORE the variable, 1 is added/subtracted from the variable BEFORE the expression is evaluated. In the case where it is placed AFTER the variable, 1 is added/subtracted from the variable AFTER the expression is evaluated. Try an explanation under link below:
http://www.nmr.mgh.harvard.edu/C/incr_decr.shtml
If seems to me that some of this might be compiler-specific because I got different output on M$VC++ from that of DevC++, and different from yours.
>>which just happens to grab info from random memory addresses since you never initialized the pointers.
I'm pretty sure the pointers are initilized in the line:"print(int *a, int *b, int *c, int *d, int *e)"