I am doing an exercise where the task is to fix swap_pointer()
.
Two string should be swapped.
s1 = "I should print second.";
s2 = "I should print first.";
My work so far:
#include <stdio.h>
void swap_nums(int *, int *);
void swap_pointers(char *x, char *y);
int main(void) {
int a = 3, b = 4;
char *s1, *s2;
swap_nums(&a, &b);
printf("a is %d\n", a);
printf("b is %d\n", b);
s1 = "I should be printed second.";
s2 = "I should be printed first.";
/* swap_pointers(s1, s2); */
printf("s1 is %s\n", s1);
printf("s2 is %s\n", s2);
return 0;
}
void swap_nums(int *x, int *y) {
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
void swap_pointers(char *x, char *y) {
char tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
I think that not appending &
to s1
and s2
before I pass them to the function for swap_pointers
is the error?
If anyone can fix my issue here, please don't give the whole solution! I want to use what you give me as a tool to fix it by my own. If I really can't fix it by my own I will adress this later.
Ty in advance!
CodePudding user response:
Let's at first consider this code snippet
int a = 3, b = 4;
swap_nums(&a, &b);
To swap the objects a
and b
you need to pass then to the function swap_nums
by reference through pointers to them. Thus within the function dereferencing the pointers we can get a direct access to original objects and change their values.
void swap_nums(int *x, int *y) {
int tmp;
tmp = *x;
*x = *y;
*y = tmp;
}
The same we need to do with the variables s1
and s2
. That is we need to pass them to the function swap_pointers
by reference through pointers to them.
So you need to write
swap_ponters( &s1, &s2 );
and the function will be declared and defined like
void swap_ponters(char **x, char **y)
{
char *tmp = *x;
*x = *y;
*y = tmp;
}
In general if you have two objects like
T a;
T b;
where T
is some type specifier then to change the objects in a function you need to pass them to the function by reference through pointers to them. So the declaration of the function swap
will look like
void swap( T *a, T *b );
and the function will be called like
swap( &a, &b );
In your program a
and b
has the type int
, that is T
is an alias for the type int
.
typedef int T;
T a = 3, b = 4;
so the function declaration will look like
void swap_nums( T *x, T *y );
For the variables s1
and s2
you can write
typedef char *T;
T s1, s2;
and again the function declaration will look like
void swap_pointers( T *x, T *y );
and the function will be called like
swap_pointers( &s1, &s2 );
As in this case T
is an alias for the type char *
then the function declaration can be rewritten like
void swap_pointers( char * *x, char * *y );
CodePudding user response:
Your pass the pointers to the first characters of two null-terminated strings to the swap_pointers
function.
Inside the function, when you use e.g. *x
it's the same as x[0]
.
So you're swapping the first character of x
with the first character of y
.
If you want to switch pointers, you need to emulate pass-by-reference like you do with swap_nums
, and pass pointers to the pointers, i.e. char **
. And use the pointer-to operator &
in the call.
So it should be
void swap_ponters(char **x, char **y);
and
swap_pointers(&s1, &s2);