Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Community > MacNN Lounge > String manipulation with C and the seg faults :(

String manipulation with C and the seg faults :(
Thread Tools
jagga
Senior User
Join Date: Jan 2000
Location: davis, CA, USA
Status: Offline
Reply With Quote
Jun 6, 2002, 12:16 PM
 
Does anyone know why I can't do the below commented lines without getting a seg fault?


void reverse (char *text)
{
int i = 0,j=0;
char swap;

for (i = strlen(text) - 1; i >= (strlen(text) / 2); i--)
{
swap = text[j];
// text[j] = text[i];
// text[i] = swap;
j++;
}
}


The fucntion has these characteristics:
input: "this is a text" THIS IS OF TYPE char * not Char []
ouput: "tset a si siht"

but the two commented lines give me seg faults!! aaahhh

Thanks

<small>[ 06-06-2002, 12:35 PM: Message edited by: jagga ]</small>
     
starman
Clinically Insane
Join Date: Jun 2000
Location: Union County, NJ
Status: Offline
Reply With Quote
Jun 6, 2002, 12:39 PM
 
Worked for me. Are you sure that *text isn't null?

Mike

Home - Twitter - Sig Wall-Retired - Flickr
     
jagga  (op)
Senior User
Join Date: Jan 2000
Location: davis, CA, USA
Status: Offline
Reply With Quote
Jun 6, 2002, 02:24 PM
 
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">quote:</font><hr /><font size="1" face="Geneva, Verdana, Arial, sans-serif">Originally posted by starman:
<strong>Worked for me. Are you sure that *text isn't null?

Mike</strong></font><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">It works if the string i am passing in is of type 'char []' as opposed to 'char *'.
     
theolein
Addicted to MacNN
Join Date: Feb 2001
Location: zurich, switzerland
Status: Offline
Reply With Quote
Jun 6, 2002, 09:37 PM
 
You have a problem with your strings. In C, if you assign a string in the form char *teststring = "some text"; it get placed in the character constant table. That means that it is unchangeable. You were basically trying to assign values to constants in the lines:
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">
text[j] = text[i];
text[i] = swap;</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">Here I have written another version that will work, albeit somewhat differently.
</font><blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">code:</font><hr /><pre style="font-size:x-small; font-family: monospace;">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
void *reverse ( const char *text,char *buffer)
{
int i = 0,j=0, len= strlen(text);
char swap;

for (i = len - 1; i &gt;= 0; i--)
{
buffer[j] = text[i];
j++;
}
buffer[len]='\0';
return buffer;
}
char *mystring =&quot;this is a text&quot; , returnstring[100];
int main (){
reverse(mystring,returnstring);
printf(&quot;%s\n&quot;,returnstring);
}
</pre><hr /></blockquote><font size="1" face="Geneva, Verdana, Arial, sans-serif">However you will not be able to get around having to use a buffer of a certain size, and although I haven't done this here, you should always check to make sure that it doesn't get overun. However if you do forget to check your array bounds and buffers, don't worry about it. Thousands of Microsoft coders seem to make a nice living coding in that very style <img border="0" title="" alt="[Wink]" src="wink.gif" />

p.s.You should post things like this in OSX - Developers. You're more likely to get help there.
weird wabbit
     
theolein
Addicted to MacNN
Join Date: Feb 2001
Location: zurich, switzerland
Status: Offline
Reply With Quote
Jun 6, 2002, 09:41 PM
 
F**ken MacNN breaking UBB when it was working just fine before!

<small>[ 06-06-2002, 09:45 PM: Message edited by: theolein ]</small>
weird wabbit
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 06:15 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,