using a swap function with pointers to array as parameters

can someone explain if im doing this right?

i need to use pointer artimetic to change the 3rd element with the 16th element

swap(int*p, int size)
{
int temp = 0;
//also i need to tell the compiler which array to use
//swap 3rd element with 15th
for(int ix =0; ix <size; ix++)
{
*p[2] = temp;
or
*(p+2) = temp;
*p[14] = *p[2];
or
*(p+15) = *(p+2);
*p[2] = *p[14];
or
*(p+2) = *(p+15);
}
}**
Please use code tags. http://www.cplusplus.com/articles/jEywvCM9/
You can edit your post, highlight your code and click the <> button on the right.

Think of how you would swap 2 numbers, passed in as pointers.
1
2
3
4
5
6
void swap( int* a, int* b )
{
    int* temp = a;
    *a = *b;
    *b = *temp;
}

Apply this logic to swap element 3 with element 16.

p[n] == *(p + n)
 
*p[2] = temp;

p[2] gives an int. You are trying to dereference an int, which won't work.
Topic archived. No new replies allowed.