Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The first thing you need to understand is that when you pass something to a function, that something is copied to the function's arguments.</p> <p>Suppose you have the following:</p> <pre><code>void swap1(int a, int b) { int temp = a; a = b; b = temp; assert(a == 17); assert(b == 42); // they're swapped! } int x = 42; int y = 17; swap1(x, y); assert(x == 42); assert(y == 17); // no, they're not swapped! </code></pre> <p>The original variables will not be swapped, because their values are copied into the function's arguments. The function then proceeds to swap the values of those arguments, and then returns. The original values are not changed, because the function only swaps its own private copies.</p> <p>Now how do we work around this? The function needs a way to refer to the original variables, not copies of their values. How can we refer to other variables in C? Using pointers.</p> <p>If we pass pointers to our variables into the function, the function can swap the values in <em>our</em> variables, instead of its own argument copies.</p> <pre><code>void swap2(int* a, int* b) { int temp = *a; *a = *b; *b = temp; assert(*a == 17); assert(*b == 42); // they're swapped! } int x = 42; int y = 17; swap2(&amp;x, &amp;y); // give the function pointers to our variables assert(x == 17); assert(y == 42); // yes, they're swapped! </code></pre> <p>Notice how inside the function we're not assigning to the pointers, but assigning to what they point to. And the pointers point to our variables <code>x</code> and <code>y</code>. The function is changing directly the values stored in <em>our</em> variables through the pointers we give it. And that's exactly what we needed.</p> <p>Now what happens if we have two pointer variables and want to swap the <em>pointers</em> themselves (as opposed to the values they point to)? If we pass pointers, the pointers will simply be copied (not the values they point to) to the arguments.</p> <pre><code>void swap3(int* a, int* b) { int* temp = a; a = b; b = temp; assert(*a == 17); assert(*b == 42); // they're swapped! } void swap4(int* a, int* b) { int temp = *a; *a = *b; *b = temp; assert(*a == 17); assert(*b == 42); // they're swapped! } int x = 42; int y = 17; int* xp = &amp;x; int* yp = &amp;y; swap3(xp, yp); assert(xp == &amp;x); assert(yp == &amp;y); assert(x == 42); assert(y == 17); // Didn't swap anything! swap4(xp, yp); assert(xp == &amp;x); assert(yp == &amp;y); assert(x == 17); assert(y == 42); // Swapped the stored values instead! </code></pre> <p>The function <code>swap3</code> only swaps its own private copies of our pointers that it gets in its arguments. It's the same issue we had with <code>swap1</code>. And <code>swap4</code> is changing the values our variables point to, not the pointers! We're giving the function a means to refer to the variables <code>x</code> and <code>y</code> but we want them to refer to <code>xp</code> and <code>yp</code>.</p> <p>How do we do that? We pass it their addresses!</p> <pre><code>void swap5(int** a, int** b) { int* temp = *a; *a = *b; *b = temp; assert(**a == 17); assert(**b == 42); // they're swapped! } int x = 42; int y = 17; int* xp = &amp;x; int* yp = &amp;y; swap5(&amp;xp, &amp;yp); assert(xp == &amp;y); assert(yp == &amp;x); assert(x == 42); assert(y == 17); // swapped only the pointers variables </code></pre> <p>This way it swaps our pointer variables (notice how <code>xp</code> now points to <code>y</code>) but not the values they point to. We gave it a way to refer to our pointer variables, so it can change them!</p> <p>By now it should be easy to understand how to swap two strings in the form of <code>char*</code> variables. The swap function needs to receive pointers to <code>char*</code>.</p> <pre><code>void swapStrings(char** a, char** b){ char *temp = *a; *a = *b; *b = temp; assert(strcmp(*a, "world") == 0); assert(strcmp(*b, "Hello") == 0); } char* x = "Hello"; char* y = "world"; swapStrings(&amp;x, &amp;y); assert(strcmp(x, "world") == 0); assert(strcmp(y, "Hello") == 0); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload