Sudhanshu Gupta
Sudhanshu Gupta

Reputation: 2315

String swap by swap function

I have written code to swap strings, but I am not able to swap. What is the problem and how can I solve it by using a function, swap?

#include <stdio.h>

void swap( char*,char*);
int main()
{
    char *ptr[2] = {"hello", "morning"};
    swap(ptr[0], ptr[1]);
    printf("%s %s", ptr[0], ptr[1]);
    return 0;
}

void swap(char *t1, char*t2)
{
    char *t;
    t = t1;

    t1 = t2;
    t2 = t;
}

I also tried to pass (&ptr[0], &ptr[1]), but here it shows a segmentation fault. Also I made a char, *p1 = ptr[0], char *p1 = ptr[1], and then passes &p1, and &p2 to swap, but still I get a segmentation fault.

Upvotes: 1

Views: 5940

Answers (4)

rashok
rashok

Reputation: 13424

char *ptr[2] = {"hello","mornig"}; - This statement means you are allocating an array of size 2 which is going to store a address of two strings(ie char *). Now the two strings you are giving will be in text segment which is a read only data. If we tries to modify it, which will leads to crash(sgmentation fault).

So if you call swap(&ptr[0], &ptr[1]) leads to a crash. because you are trying to write the charcter m in the first read only string h. Write is not possible to the string which is in text segment.

If you want to simply swap a string you can call the function as swap(&ptr[0], &ptr[1]), which is equivalent to swap((ptr + 0),(ptr + 1));.

And change the swap function as below

void swap( char **t1,char **t2)
{
        char *t;

        t = *t1;
        *t1 = *t2;
        *t2 =t;
}

Here we are just swapping the address of the string stored in the array pointer ptr.

Upvotes: 1

pb2q
pb2q

Reputation: 59617

C function arguments are pass-by-value. You're passing the value of addresses to your swap function, and expecting those values, the addresses, to change. But only the copies of the addresses in your swap function change.

To change the actual passed-in addressed, you'll need an aditional level of reference:

void swap(char **t1, char **t2)
{
    char *t;

    t = *t1;
    *t1= *t2;
    *t2 = t;
}

And call this swap like so: swap(&ptr[0], &ptr[1]);

Upvotes: 8

Man of One Way
Man of One Way

Reputation: 3980

You need to pass in pointers to the pointers of the char arrays in order to switch them. By doing that you can swap the values of those pointers instead. (Which are the actual arrays.)

#include<stdio.h>
void swap( char**,char**);
int main()
{
        char *ptr[2] = {"hello","mornig"};
        swap(&ptr[0], &ptr[1]);
        printf("%s %s",ptr[0], ptr[1]);
        return 0;
}

void swap( char **t1,char **t2)
{
        char *t;
        t = *t1;

        *t1 = *t2;
        *t2 =t;
}

Upvotes: 6

perreal
perreal

Reputation: 97948

You need to pass pointers to your strings to swap them (char**). Otherwise the changes you do will be local to the swap function.

void swap( char **t1,char **t2)
{
        char *t;
        t = *t1;

        *t1 = *t2;
        *t2 =t;
}

int main()
{
        char *ptr[2] = {"hello","mornig"};
        swap(&ptr[0], &ptr[1]);
        printf("%s %s",ptr[0],ptr[1]);
        return 0;
}

Upvotes: 3

Related Questions