1

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!

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
I Y S Z
  • 45
  • 5

2 Answers2

3

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 );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Any good source where I can learn that stuff? Currently learning with EDX and a PDF file to do some pratice. – I Y S Z Feb 10 '22 at 13:09
  • @IYSZ I am sorry. I even do not remember when I read a book on C last time.:) – Vlad from Moscow Feb 10 '22 at 13:18
  • @IYSZ - the take-away is that at some point a good book (or several) _was_ read... Google Books on the C programming language, read the reviews and pick one (or several) Some sites will have them available as used for greatly reduced prices if that is important to you. – ryyker Feb 10 '22 at 13:25
  • @ryyker TY, Ill take a look onto this – I Y S Z Feb 10 '22 at 13:39
  • @IYSZ Or look [here](https://stackoverflow.com/q/562303/11294831). – the busybee Feb 10 '22 at 15:23
2

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);
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • I was like "string MUST BE working like arrays" and you made me think now how dumb i am lmao ty! – I Y S Z Feb 10 '22 at 13:00
  • So it worked. But i still get GCC Warnings. – I Y S Z Feb 10 '22 at 13:01
  • @IYSZ Actually, literals strings *are* arrays. They, like other arrays, can decay to pointers to their first element. Doing `char s1_arr[] = "hello"; char *s1 = s1_arr;` is somewhat similar to `char *s1 = "hello";`. – Some programmer dude Feb 10 '22 at 13:05
  • In continuation to what @Someprogrammerdude commented about string literals and arrays: String literals are constant strings while the char array is isn't unless you make it a `const` variable. (This touch-bases on whats implied by "somewhat similar" in the comment above). – WedaPashi Feb 10 '22 at 13:08
  • @IYSZ What warnings? Please edit your question and add your new code and the warnings, but don't remove the old one. – the busybee Feb 10 '22 at 15:22
  • @the busybee ive fixed the warning. i missed right naming – I Y S Z Feb 11 '22 at 11:08