473,387 Members | 1,903 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

String swapping problem

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
Nov 14 '05 #1
7 7360

<an*******@coolgroups.com> a écrit dans le message de
news:54******************************@news.scbiz.c om...
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
Nov 14 '05 #2
an*******@coolgroups.com wrote:
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

Sha


Nov 14 '05 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

an*******@coolgroups.com wrote:
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()).
}
Sha

- --
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-----
Nov 14 '05 #4
In article <54******************************@news.scbiz.com >,
an*******@coolgroups.com wrote:
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,
--
Rob van der Leek | rob(at)ricardis(dot)tudelft(dot)nl
Ricardishof 73-A | http://www.ricardis.tudelft.nl/~rob
2614 JE Delft, The Netherlands
+31 (0)6 155 244 60
Nov 14 '05 #5
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;

}
Nov 14 '05 #6

"A" <fe********@aliceposta.com> a écrit dans le message de
news:6s***********************@news4.tin.it...

Hi,
Here's the code

#include <stdio.h>

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


No, that's C++ stuff. There are no references in C.

[snipped]

Regis
Nov 14 '05 #7
A
Sorry I didn't know
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Christopher Jeris | last post by:
Please help me understand the differences, in semantics, browser support and moral preferredness, between the following three methods of swapping content in and out of a page via JavaScript. I...
16
by: Sethu Madhavan | last post by:
Hi All Here i am facing one technical issue related to Swapping the Object Instance in Dotnet. e.g.. class Customer{ Order _lastOrder;
270
by: Jatinder | last post by:
I found these questions on a web site and wish to share with all of u out there,Can SomeOne Solve these Porgramming puzzles. Programming Puzzles Some companies certainly ask for these...
28
by: rajendra.stalekar | last post by:
Hi Folks!!! I have a string let's say "hi" and got to reverse it using just a single variable for swapping, how do I do it? Regards, Rajendra S.
8
by: meendar | last post by:
This is my program on swap of strings.i just used pointer for swapping the string. can anyone help me why it is not working? #include<iostream.h> #include<string.h> void swap(char *char1,...
34
by: Ann | last post by:
I am opening a file which looks like 0xABCDEF01 on another machine but 0x01EFCDAB on my machine. Is this a byte swapping? Could anyone give a good way to check if bytes are being swapped?...
4
by: Jonathan Wood | last post by:
Is it just me? It seems like one moving from MFC to C# loses some string functionality such as the two mentioned in the subject. Did I miss something? -- Jonathan Wood SoftCircuits...
111
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
14
by: Javier | last post by:
Hello, in which cases is it better the use of "const char*" to "string" (or even const string &). I mean, in STL (http://www.sgi.com/tech/stl/hash_map.html) I see: hash_map<const char*, int,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.