String Swap Not Working

Reply

Join Date: Jul 2009
Posts: 9
Reputation: rcbhat is an unknown quantity at this point 
Solved Threads: 1
rcbhat rcbhat is offline Offline
Newbie Poster

String Swap Not Working

 
0
  #1
Jul 11th, 2009
Hi, this is the code that I have to swap two strings-

  1. void swap(char *,char *);
  2.  
  3. main()
  4. {
  5. char *x="new";
  6. char *y="word";
  7. char *t;
  8.  
  9. swap(x,y);
  10. printf("(%s,%s)",x,y);
  11.  
  12. }
  13.  
  14.  
  15. void swap(char *x,char *y)
  16. {
  17. char *t;
  18.  
  19. t=x;
  20. x=y;
  21. y=x;
  22.  
  23.  
  24.  
  25. }

output- (new,word)

Why is the swap function not swapping the strings once the control comes out of swap? I am passing it using pointers (by reference).
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: String Swap Not Working

 
0
  #2
Jul 11th, 2009
Because you're only swapping within the scope of the function. Instead you need to pass the address of the pointers, not the memory referenced by those pointers!

Despite that, line 21 was wrong!


  1. void swap(char *,char *);
  2.  
  3. main()
  4. {
  5. char *x="new";
  6. char *y="word";
  7.  
  8. swap( &x, &y );
  9.  
  10. printf("(%s,%s)",x,y);
  11. }
  12.  
  13. void swap( char **x, char **y)
  14. {
  15. char *t;
  16.  
  17. t = *x;
  18. *x = *y;
  19. *y = t;
  20. }
Last edited by wildgoose; Jul 11th, 2009 at 7:37 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: String Swap Not Working

 
0
  #3
Jul 11th, 2009
And don't forget to change line 1 of your code to:
void swap(char **,char **);
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: String Swap Not Working

 
0
  #4
Jul 11th, 2009
oops, good catch tux4life.

I personally hate prototypes unless they're in header files. I intentionally order my functions in dependence order thus main is at the bottom of the file. But didn't do it in this case!
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,343
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 237
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: String Swap Not Working

 
0
  #5
Jul 12th, 2009
Originally Posted by rcbhat View Post
Hi, this is the code that I have to swap two strings
Note also that you are not swapping strings; you are swapping pointers.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 9
Reputation: rcbhat is an unknown quantity at this point 
Solved Threads: 1
rcbhat rcbhat is offline Offline
Newbie Poster

Re: String Swap Not Working

 
0
  #6
Jul 12th, 2009
Thanks for all your help, I realized that you cannot actually 'pass by reference' in C, and the pointer manipulation is a cool way to accomplish the effect. That is what I was missing.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,612
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 463
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: String Swap Not Working

 
2
  #7
Jul 12th, 2009
rcbhat,
Array of chars and pointer to chars is not same thing.
Declaration of your code is pointer to chars.
  1. char *x="new";
  2. char *y="world";
  3. ...
'x' points to the first char of "new" which is an
array of constant chars in C++. A special rule in C++ allows the
assignment of the address of a string literal (array of const char) to
a pointer to modifiable char(s). Attempting to modify any const
object in C++, including string literals, produces undefined behavior.

If you are talking about C, not C++, the type of the string literal is
array of chars and NOT array of constant chars. Attempting to modify
a string literal in C produces undefined behavior not because they are const qualified chars, but because the C standard specifically states that it is undefined behavior.

http://c-faq.com/charstring/index.html
http://www.parashift.com/c++-faq-lite/
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

OP has another thread with different question for pointer to characters. - http://www.daniweb.com/forums/thread203375.html
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 9
Reputation: rcbhat is an unknown quantity at this point 
Solved Threads: 1
rcbhat rcbhat is offline Offline
Newbie Poster

Re: String Swap Not Working

 
0
  #8
Jul 12th, 2009
Originally Posted by adatapost View Post
Attempting to modify a string literal in C produces undefined behavior not because they are const qualified chars, but because the C standard specifically states that it is undefined behavior.
In the swap function that I wrote, I did not try to modify the string literal, I just wanted to interchange the pointers to the array of chars.

I realized that this cannot be accomplished using pass by values unless I am able to work with the actual pointers to the pointers to the chars (i.e. the pointers to the first elements of the string literals)

So, correct me if you think I may be wrong, but your reply is out of context for the question.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: String Swap Not Working

 
0
  #9
Jul 13th, 2009
Originally Posted by rcbhat View Post
So, correct me if you think I may be wrong, but your reply is out of context for the question.
it's correct, and totally within context.

because your original question and code made it appear that you were operating on a misconception about string literals. even if you did not actually have this misconception, it's a common problem and an unclear question could easily confuse others.

since these threads here will be available for a long time, his clarification is as much for future reference and is entirely appropriate.
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 5
Reputation: Kurt Kubing is an unknown quantity at this point 
Solved Threads: 0
Kurt Kubing Kurt Kubing is offline Offline
Newbie Poster

Re: String Swap Not Working

 
0
  #10
Jul 14th, 2009
I think there's only one reason why your code will never work as you expected: you declare read-only strings. Although you didn't declare it with the const keyword, declaring string that way makes your string variable untouchable.

Here are my suggestions:

You either write your code this way:

1.... char szStr1[] = "Hello";
2.... char szStr2[] = "About";

Take note that your strings have the same length. Although it should not be necessary, you have to make sure your string arrays' length can accomodate the length of the longer one.

3.... void strSwap(char* str1, char* str2)
{
char szTemp[1024];
strcpy(szTemp, str1);
strcpy(str1, str2);
strcpy(str2, szTemp);
}

...or write your function this way:
1.... void strSwap(char** ppStr1, char** ppStr2)
{
char * szTemp;
int cch1, cch2;
cch1 = strlen(*ppStr1);
cch2 = strlen(*ppStr2);
szTemp = (char*)malloc(cch1+1);
strcpy(szTemp, *ppStr1);
*ppStr1 = (char*)malloc(cch2+1);
strcpy(*ppStr1, *ppStr2);
*ppStr2 = szTemp;
}

In the method, you have to declare you strings like this:
char * szStr1 = "Help";
char * szStr2 = "Anonymous";
strtSwap(&szStr1, &szStr2);
...
// use your szStr1 and szStr2
...
free(szStr1);
free(szStr2);

Well, the second method I think is costly. I just hope I can help you solve, or at least give a hint in solving your problem.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC