Changing string

Y

yaru22

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
 
E

Eric Sosman

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/>.
 
A

Arun

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.

=> Specify to your compiler that this is a writable string by
=> char str[] = "\"hello\"";
This is Question 1.32 in the comp.lang.c Frequently
Asked Questions (FAQ) list at <http://www.c-faq.com/>.

-Arun Joseph
 
F

Flash Gordon

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top