3

I have a function which takes a pointer to an array (so an int**). In this function, I'd like to call swap(int*, int*) to swap the location of two of the elements in the array. What is the syntax in C for swapping these two elements?

Here's an example of what I'm looking for:

int* do_something(int** arr) {
    // assume i and j are valid locations in the array
    swap(&arr[i], &arr[j]); // what should this line be?
}

// this function works fine, no changes needed
void swap(int* a, int* b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}
7
  • 1
    just remove & symbols Commented Oct 14, 2014 at 18:35
  • You need to dereference the pointer in do_something. Commented Oct 14, 2014 at 18:36
  • 1
    For future reference, the term "double pointer" might be misconstrued as: double* a;. Though, on the other hand, saying "pointer to a pointer to an integer" is a bit wordy. Commented Oct 14, 2014 at 18:37
  • I actually ran into that exact problem when I was googling this earlier. Thanks for pointing that out, it could be confusing. Commented Oct 14, 2014 at 18:40
  • You are not passing pointer to array, you are passing pointer to pointer. Note that it's very rare to pass pointer to array in C, you usually pass pointer to first item in the array. Commented Oct 14, 2014 at 18:53

3 Answers 3

4

It's not clear to me what arr is. Is it an array of int*, or is it a pointer to an array of int?

Option 1: arr is an array of int*:

Your swap function swaps int variables, but you need to swap int* variables. You therefore need an extra level of indirection in the swap function.

void swap(int** a, int** b) 
{
    int* temp = *a;
    *a = *b;
    *b = temp;
}

Option 2: arr is a pointer to an array of int:

In this case, you wish to swap two int values, and the variant of swap that appears in the question does just that. The problem is that you have defined do_something incorrectly. It should receive an int* and be implemented like this:

void do_something(int* arr, int i, int j) 
{
    swap(&arr[i], &arr[j]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Option 2 is what I am attempting to accomplish. By redefining do_something the way you showed, it can make changes to the original array right? So back in the function that calls do_something, the swap should show up?
0

The line

swap(&arr[i], &arr[j]);

should be something like

swap( ((*arr) + i), ((*arr) + j) );

Since arr points to the base address of the array, dereferencing it makes it same as the base address of the array, then incrementing by i or j would make it point to the corresponding element.

Comments

0

Just remove & symbols. Array elements are already accessed using pointers.

When you say

arr[i] 

you are actually saying

*(arr + i)

So you are already passing a pointer to do_something.

Since arr contains pointers to integers (int*), then saying arr[i] you are accessing the int* at i-th position so you can just pass it into your swap method that accepts int *

3 Comments

arr is a pointer to pointer to int. wont doing arr+1 make arr increment by 8 bytes (if address size is 8)? i think what he needs is (*arr) + i)
derefencing a pointer to pointer to int once makes it point to the value??
Actually, in this case since it is an array of int *, I think both of us are correct. Sorry

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.