|
|
String manipulation with C and the seg faults :(
|
|
|
|
Senior User
Join Date: Jan 2000
Location: davis, CA, USA
Status:
Offline
|
|
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>
|
|
|
|
|
|
|
|
|
Clinically Insane
Join Date: Jun 2000
Location: Union County, NJ
Status:
Offline
|
|
Worked for me. Are you sure that *text isn't null?
Mike
|
|
|
|
|
|
|
|
|
Senior User
Join Date: Jan 2000
Location: davis, CA, USA
Status:
Offline
|
|
</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 *'.
|
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Feb 2001
Location: zurich, switzerland
Status:
Offline
|
|
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 <stdio.h>
#include <string.h>
void *reverse ( const char *text,char *buffer)
{
int i = 0,j=0, len= strlen(text);
char swap;
for (i = len - 1; i >= 0; i--)
{
buffer[j] = text[i];
j++;
}
buffer[len]='\0';
return buffer;
}
char *mystring ="this is a text" , returnstring[100];
int main (){
reverse(mystring,returnstring);
printf("%s\n",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
|
|
|
|
|
|
|
|
Addicted to MacNN
Join Date: Feb 2001
Location: zurich, switzerland
Status:
Offline
|
|
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 Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|