Switch first and last name to C

My question is to understand why this is happening.

My goal is to pass the name “Joe” and the last name “Bloggs” into the swap function I created and enable it. (So, the first name will now contain Bloggs the other way around)

I created a swap function that takes pointers to first and last name as arguments. The function works because, using the print operator, I see that the values ​​of the two lines have been changed.

#include <stdio.h>

void swap(char * first, char * last) {
    char * temp;
    temp = first;
    first = last;
    last = temp;
    printf("at end of swap func the first name is: %s\n", first);
    printf("at end of swap func the last name is: %s\n", last);
}

Now the problem is that after calling the swap function, I print the first and last name again, but they do not display the results of the swap function.

int main() {
    char * first = "Joe";
    char * last = "Bloggs";
    printf("The original format: %s %s\n", first, last);
    swap(first, last);
    printf("The new format: %s %s", first, last);//new format was supposed to be Bloggs Joe
}

SO, , , . , , . C, .

+4
3

C . , first last first last main. .

, . , , .

, :

void swap(char ** first, char ** last) {
  char * temp;
  temp = *first;
  *first = *last;
  *last = temp;
  printf("at end of swap func the first name is: %s\n", *first);
  printf("at end of swap func the last name is: %s\n", *last);
}

:

swap(&first, &last);

, , , - , , . , , , , .

char, , . , .

, :

#include <stdio.h>
#include <string.h>

// assumes that each array is big enough to hold the contents of the other
void swap(char *first, char *last) {
  int len1 = strlen(first);
  int len2 = strlen(last);
  int maxlen = (len1 > len2) ? len1 : len2;
  char temp[maxlen+1];
  strcpy(temp, first);
  strcpy(first, last);
  strcpy(last, temp); 
  printf("at end of swap func the first name is: %s\n", first);
  printf("at end of swap func the last name is: %s\n", last);
}

int main() {
  char first[10] = "Joe";
  char last[10] = "Bloggs";
  printf("The original format: %s %s\n", first, last);
  swap(first, last);
  printf("The new format: %s %s", first, last);
}
+11

:

char *temp;
temp = first;
first = last;
last = first;

C, pass by value. , . , , , , .

#define swap(A, B) do {            \
    char **swap_A__ = &(A);        \
    char **swap_B__ = &(B);        \
    char *swap_temp__ = *swap_A__; \
    *swap_A__ = *swap_B__;         \
    *swap_B__ = swap_temp__;       \
} while (0)

, .

, . :

void swap_generic(void *a, void *b, size_t sz);

, , " ", :

#define swap(A, B) do {                                    \
    _Static_assert(sizeof(A) == sizeof(B), "unswappable"); \
    _Static_assert(sizeof(&(A) - &(B)), "type check");     \
    swap_generic(&(A), &(B), sizeof(A));                   \
} while (0)

:

void swap_generic (void *a, void *b, size_t sz) {
    char *aa = a;
    char *bb = b;
    while (sz--) {
        char temp = *aa;
        *aa++ = *bb;
        *bb++ = temp;
    }
}
+1
#include <stdio.h>

void swap(char ** first, char ** last) {
  char * temp;
  temp = *first;
  *first = *last;
  *last = temp;
  printf("at end of swap func the first name is: %s\n", *first);
  printf("at end of swap func the last name is: %s\n", *last);
}

int main() {
  char * first = "Joe";
  char * last = "Bloggs";
  printf("The original format: %s %s\n", first, last);
  swap(&first, &last);
  printf("The new format: %s %s", first, last);//new format was supposed to be Bloggs Joe
}

, , .

0
source

Source: https://habr.com/ru/post/1696333/


All Articles