String swapping problem

anonymous_at_coolgroups.com
Date: 05/17/04


Date: Mon, 17 May 2004 12:39:34 GMT

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



Relevant Pages

  • Re: copymemory basic question
    ... it seems that the optimizer in VB copies the pointers ... to strings instead of swapping the contents. ... Dim temp As String ...
    (microsoft.public.vb.winapi)
  • Re: beginner string question
    ... > a compile error. ... Use std::string instead of plain C-style strings. ... If you declared temp ... result of comparing will be always false. ...
    (comp.lang.cpp)
  • Re: beginner string question
    ... Sorry but I gave the wrong info, I declared "temp" as a character array if ... I did use strings but they were breaking up in ... > a compile error. ...
    (comp.lang.cpp)
  • Re: String() declaration?
    ... the temp array is holding strings ... Dim temp as String ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Problem with this text-sorting algorithm
    ... I need to write an algorithm which will sort an array of text strings ... I'd done the sort. ... Dim temp As String ...
    (microsoft.public.vb.general.discussion)

Loading