String swapping problem

A

anonymous

Hi CLCers,
I tried the following code for swapping a string, but it is
not working. Inside the swap function the strings are
printed correctly, but when back in main() the strings are
not swapped at all. Thanks in advance. Here is the code.

#include<stdio.h>
int main()
{
void swap(char * string1, char * string2);

char * string1 = "Hello World";
char * string2 = "Hello Jupiter";
swap(string1, string2);
printf("%s\n%s\n",string1,string2);
return 0;
}


void swap( char * string1, char * string2)
{
char * temp;
temp = string1;
string1 = string2;
string2 = temp;
printf("%s\n%s\n",string1,string2);
}


Sha
 
R

Régis Troadec

Hi CLCers,
Hi,

I tried the following code for swapping a string, but it is
not working. Inside the swap function the strings are
printed correctly, but when back in main() the strings are
not swapped at all. Thanks in advance. Here is the code.

#include<stdio.h>
int main()
{
void swap(char * string1, char * string2);

char * string1 = "Hello World";
char * string2 = "Hello Jupiter";
swap(string1, string2);
printf("%s\n%s\n",string1,string2);

The strings string1 and string2 you're printing here are those which are in
the main() scope.
return 0;
}


void swap( char * string1, char * string2)
{
char * temp;
temp = string1;
string1 = string2;
string2 = temp;
printf("%s\n%s\n",string1,string2);

The strings string1 and string2 you're printing here are those which are in
the swap() scope (local copies of the string1 and string2 pointers).

Regis
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi CLCers,
I tried the following code for swapping a string, but it is
not working. Inside the swap function the strings are
printed correctly, but when back in main() the strings are
not swapped at all. Thanks in advance. Here is the code.

See my comments below...
#include<stdio.h>
int main()
{
void swap(char * string1, char * string2);

char * string1 = "Hello World";
char * string2 = "Hello Jupiter";

You have two pointers (string1 and string2), each pointing to something that
cannot be altered. This probably isn't what you want, but it's what you asked for.
swap(string1, string2);

In C, all function calls are 'call-by-value'.
This invocation of swap() passes the swap() function a /copy/ of the string1
pointer, and a /copy/ of the string2 pointer. Neither the string1 nor the
string2 pointer are passed directly to the swap() function, and swap() will not
affect either pointer.
printf("%s\n%s\n",string1,string2);

string1, not having been changed since it's inception, still points at the
unalterable string "Hello World". Likewise, string2 still points at the
unalterable string "Hello Jupiter". And that's what you get from this printf().
return 0;
}


void swap( char * string1, char * string2)
{

swap() is acting on local copies of the original string1 and string2 pointers.
It doesn't do anything to the original pointers.
char * temp;
temp = string1;
string1 = string2;
string2 = temp;

So, you've swapped your local copies around, so that your local variable called
string1 now contains the pointer originally obtained in your local string2
variable, and string2 now contains the pointer originally obtained in your local
string1 variable.
printf("%s\n%s\n",string1,string2);

And you print the strings pointed to by string1 (now containing a pointer to
"Hello Jupiter") and string2 (now containing a pointer to "Hello World").

You haven't changed the original pointers (declared in main()).


- --
Lew Pitcher
IT Consultant, Enterprise Application Architecture,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFAqLVVagVFX4UWr64RAhFaAKDQBtWEEJ/Tg3oFMH0/2XntvIkwSACg9ZCa
fuACUoGcxtM7UHcV/HrhNpU=
=94pq
-----END PGP SIGNATURE-----
 
C

Case

Hi CLCers,
I tried the following code for swapping a string, but it is
not working. Inside the swap function the strings are
printed correctly, but when back in main() the strings are
not swapped at all. Thanks in advance. Here is the code.

#include<stdio.h>
int main()
{
void swap(char * string1, char * string2);

char * string1 = "Hello World";
char * string2 = "Hello Jupiter";
swap(string1, string2);
printf("%s\n%s\n",string1,string2);
return 0;
}


void swap( char * string1, char * string2)
{
char * temp;
temp = string1;
string1 = string2;
string2 = temp;
printf("%s\n%s\n",string1,string2);
}

In swap() string1 and string2 are local copies of the addresses
of the two strings. So, swap() simply swaps the value of these
local copies (which are addresses); the strings themselves are
not modified at all.

In C function parameters are passed by value and not by reference.

This function would work if you'd pass the address of string1 and
string2 in main(). void swap(char **string1, char **string2) ...
Called with swap(&string1, &string2).

Kees
 
R

Rob van der Leek

Hi CLCers,
I tried the following code for swapping a string, but it is not
working. Inside the swap function the strings are printed correctly,
but when back in main() the strings are not swapped at all. Thanks in
advance. Here is the code.

int main()
{
void swap(char * string1, char * string2);

char * string1 = "Hello World";
char * string2 = "Hello Jupiter";
swap(string1, string2);
printf("%s\n%s\n",string1,string2);
return 0;
}


void swap( char * string1, char * string2)
{
char * temp;
temp = string1;
string1 = string2;
string2 = temp;
printf("%s\n%s\n",string1,string2);
}

In C parameters are passed by value, this also holds for pointers. This
means that string1 inside swap is a copy of string1 in the scope of
main. To modify the value of main's string1 (and string2) inside swap
you must pass it as a reference from main to swap, e.g.:

void swap (char **string1, char **string2)
{
char *temp;
temp = *string1;
*string1 = *string2;
*string2 = temp;
}

and call it from main() like:

swap(&string1, &string2);

Also, don't forget to include "stdio.h" since you're using printf().

Regards,
 
A

A

Here's the code

#include <stdio.h>

void swap(char *&st1, char *&st2);

int main() {

char *string1 = "Hello World";

char *string2 = "Hello Jupiter";

swap(string1, string2);

printf("%s\n%s\n",string1,string2);

return 0;

}

void swap(char* &st1, char* &st2) {

char* tmp;

tmp = st1;

st1= st2;;

st2 = tmp;

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top