0

I am trying to write a swap function, which will swap data of 2 pointers. I get segmentation fault error. Can someone help, please?

P.S. I know that string class has built in swap function but I am trying to learn how pointers work.

#include<iostream>
#include<cstring>

using namespace std;

void swap(char *, char *);

int main(){
    char *s1="blah";
    char *s2="crap";
    swap(s1, s2);
    cout<<s1<<endl<<s2<<endl;
    return 0;
}

void swap(char *s1, char *s2){
    char *t1 = new char[strlen(s1)+1];
    char *t2 = new char[strlen(s2)+1];
    for(int i=0; i<=strlen(s1); i++){
        t1[i] = s1[i];
    }
    for(int i=0; i<strlen(s2); i++){
        t2[i] = s2[i];
    }
    cout<<*t1<<endl;
    cout<<*t2<<endl;
    *s1 = *t2;
    *s2=*t1;

}
xvan
  • 4,554
  • 1
  • 22
  • 37
Yhlas
  • 411
  • 3
  • 5
  • 17
  • 1) `i `i<=strlen(s2)` 2) `*s1 = *t2;` write only one character and Changing the string literal. 3) Need `delete[]` – BLUEPIXY Apr 28 '16 at 06:37
  • Related: [Modifying String Literal](http://stackoverflow.com/q/5464183/514235). – iammilind Apr 28 '16 at 06:40
  • i<=strlen(s1) should be i – Striker Apr 28 '16 at 06:41
  • 3
    @iammilind: I don't think it is a duplicate of that question, as swapping strings is a completely different problem than swapping ints. – MikeMB Apr 28 '16 at 06:58
  • 1
    I don't think it's a duplicatte, but it's also a rather badly written question. For starters, pick a language. C and C++ are distinct languages, and both do have pointers, but stuff like `char* s1 = "blah";` isn't C++ and `std::cout<<` isn't C. – MSalters Apr 28 '16 at 10:39
  • @MikeMB, I have reopened the Q. BTW, why do you think that `std::swap` wouldn't work on this case? I still feel that this Q is a duplicate of [C++ Swapping Pointers](http://stackoverflow.com/q/15672805/514235). – iammilind Apr 28 '16 at 11:52
  • 2
    @MSalters : `char* s1 = "blah";` is valid c++ – xvan Apr 28 '16 at 11:57
  • I think it might be seg faulting because the code is not ensuring that t1 and t2 are null terminated after the last character is copied into them. – RJM Apr 28 '16 at 13:57
  • @xvan: **was** valid C++. The practice was already deprecated in C++98 but it's now banned outright. – MSalters Apr 28 '16 at 15:48
  • @iammilind: In the linked question the author has two pointers to individual variables and wants to swap their content. Here the OP has two pointers to arrays in read-only memory, whose contents he tries to swap (the reason for the segfault). I'd say it is definitively related, but imho not a duplicate and I'm not sure it would be obvious to a novice how the linked answer can be applied to the question at hand. And of course `std::swap` would work here. – MikeMB Apr 28 '16 at 19:16

1 Answers1

4

You can use swap function as

void swap(char **s1, char **s2){

    char *temp = *s1;
    *s1 = *s2;
    *s2 = temp;
}

And call this function as

 swap(&s1, &s2);

If you use c++ you can use reference version suggested by BLUEPIXY

void swap(const char *&s1, const char *&s2)
{
    const char *temp = s1;
    s1 = s2;
    s2 = temp;
}
Community
  • 1
  • 1
ashiquzzaman33
  • 5,781
  • 5
  • 31
  • 42