Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Changing string

Reply
Thread Tools

Changing string

 
 
yaru22
Guest
Posts: n/a
 
      03-06-2007
Hi, I'm relatively new to C programming and was quite confused by this
error.

What I tried to do is that if I have a string with double quotes
around it (i.e. char *str = "\"hello\"", I have to remove the
quotes.

So I programmed this as follow:

#include <stdio.h>
#include <string.h>

int change(char *str) {
int len = strlen(str);
if (str[0]=='\"' && str[len-1]=='\"') {
char str_tmp[len+1];
strncpy(str_tmp,str+1,len-2);
str_tmp[len-2]='\0';
*str = *str_tmp;
}
return 0;
}

int main() {
char *str = "\"hello\"";
change(str);
printf("%s\n",str);
return 0;
}

However, if I run this code, I get segmentation fault error.

I guess "*str = *str_tmp;" part is wrong, but I don't know how to fix
it.

Doesn't this statement mean, substitute the value of str_tmp into the
memory location that str is pointing to?

I wonder what the reason for the error and how to do it.

Also, the return type of the function "change" should be int.

Thanks in advance.

Brian

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      03-06-2007
yaru22 wrote On 03/06/07 10:27,:
> Hi, I'm relatively new to C programming and was quite confused by this
> error.
>
> What I tried to do is that if I have a string with double quotes
> around it (i.e. char *str = "\"hello\"", I have to remove the
> quotes.
>
> So I programmed this as follow:
>
> #include <stdio.h>
> #include <string.h>
>
> int change(char *str) {
> int len = strlen(str);
> if (str[0]=='\"' && str[len-1]=='\"') {
> char str_tmp[len+1];
> strncpy(str_tmp,str+1,len-2);
> str_tmp[len-2]='\0';
> *str = *str_tmp;
> }
> return 0;
> }
>
> int main() {
> char *str = "\"hello\"";
> change(str);
> printf("%s\n",str);
> return 0;
> }
>
> However, if I run this code, I get segmentation fault error.


This is Question 1.32 in the comp.lang.c Frequently
Asked Questions (FAQ) list at <http://www.c-faq.com/>.


--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Arun
Guest
Posts: n/a
 
      03-06-2007
On Mar 6, 10:31 am, Eric Sosman <(E-Mail Removed)> wrote:
> yaru22 wrote On 03/06/07 10:27,:
>
>
>
>
>
> > Hi, I'm relatively new to C programming and was quite confused by this
> > error.

>
> > What I tried to do is that if I have a string with double quotes
> > around it (i.e. char *str = "\"hello\"", I have to remove the
> > quotes.

>
> > So I programmed this as follow:

>
> > #include <stdio.h>
> > #include <string.h>

>
> > int change(char *str) {
> > int len = strlen(str);
> > if (str[0]=='\"' && str[len-1]=='\"') {
> > char str_tmp[len+1];
> > strncpy(str_tmp,str+1,len-2);
> > str_tmp[len-2]='\0';
> > *str = *str_tmp;


=> The above statement will assign the first char in str_tmp to first
char of str.
=> Use strcpy(str,str_tmp) instead. This will fix the logical error.

> > }
> > return 0;
> > }

>
> > int main() {
> > char *str = "\"hello\"";


=> Specify to your compiler that this is a writable string by
=> char str[] = "\"hello\"";

> > change(str);
> > printf("%s\n",str);
> > return 0;
> > }

>
> > However, if I run this code, I get segmentation fault error.

>
> This is Question 1.32 in the comp.lang.c Frequently
> Asked Questions (FAQ) list at <http://www.c-faq.com/>.
>
> --
> (E-Mail Removed)- Hide quoted text -
>
> - Show quoted text -


-Arun Joseph

 
Reply With Quote
 
ComerHides@gmail.com
Guest
Posts: n/a
 
      03-06-2007
Replace the code

*str = *str_tmp; by

str = str_tmp;

and check the output


 
Reply With Quote
 
Flash Gordon
Guest
Posts: n/a
 
      03-06-2007
(E-Mail Removed) wrote, On 06/03/07 16:28:
> Replace the code
>
> *str = *str_tmp; by
>
> str = str_tmp;
>
> and check the output



Firstly, please quote sufficient of what you are replying to so that
people know what you are refering to. There is no guarantee that people
have, or ever will, see the message you are replying to.

Secondly, try your suggestion and you will see that it does not work.
Then search through the comp.lang.c FAQ for the question and answer that
tells you why it does not work.
--
Flash Gordon
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting 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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing style of the numbers in a CSS <ol> without changing the style of the <li> Aaron Beall HTML 2 09-14-2007 08:07 PM
<FAQENTRY>I'm changing my page but nothing is changing on the screen. Why?</FAQENTRY> VK Javascript 3 03-15-2007 04:59 PM
changing file extension without changing filename jamy Computer Support 4 03-04-2005 01:03 AM
Help with changing char *string to int * string for sorting. Pokerkook C Programming 4 12-10-2004 01:33 PM
Difficulty in changing the Connection Server control path changing ... Samridhi Kumar Shukla ASP .Net 1 11-30-2003 02:31 AM



Advertisments