string

R

ramu

Hi all,

char *str;

str="hello";
str='b';

What's wrong in this? Am getting segmentation fault.

Regards,
 
V

Vladimir S. Oka

ramu said:
Hi all,

char *str;

str="hello";
str='b';

What's wrong in this? Am getting segmentation fault.

Regards,

If you compile in strict ANSI/ISO C mode you'll notice that the
second assignment generates a warning of the type: "assignment
makes pointer from integer without a cast" (at least in gcc).
This is no surprise, as 'b' is not a string but a character
constant, which gets automatically promoted to an int, but
you're assigning it to a char*. Your code as posted does not
actually seg-fault (once wrapped in something compilable). I
guess that in the code you did not post, you try to dereference
str.

Cheers

Vladimir
 
K

Keith Thompson

ramu said:
char *str;

str="hello";
str='b';

What's wrong in this? Am getting segmentation fault.

What's wrong with it is that you're not showing us the actual code
that's causing the segmentation fault. Please post a small, complete,
compilable program that exhibits the problem so we don't have to
guess.

If your program really contains
str='b';
then you're trying to assign an integer value (98 if you're using an
ASCII system) to a pointer variable. You can't legally do this; you
can explicitly convert an integer to a pointer using a cast, but there
is no implicit conversion. If your compiler allows it anyway (as an
extension), you're setting str to point to address 98, which is
unlikely to make any sense -- but it *probably* won't cause a
segmentation fault unless you try to use the pointer value.

If your program really contains either
*str='b';
or
str[0]='b';
then you should read question 1.32 in the FAQ at <http://c-faq.com>.
 
R

ramu

My actual code is like this:

main()
{
char *str;


str="hai";

*str='b';
printf("%s",str);
}

I want to replace the character "h" with "b". How can I do it?
 
V

Vladimir S. Oka

ramu said:
My actual code is like this:

main()
{
char *str;


str="hai";

*str='b';
printf("%s",str);
}

I want to replace the character "h" with "b". How can I do it?

As Keith said elsethread, you should read question 1.32 in the
FAQ at <http://c-faq.com>.

A quick and dirty answer: str = "hai" assigns a pointer to
a /constant/ string literal "hai" to str. Your next line tries
to modify a constant.

What you probably want is:

char str[] = "hai";

*str = 'b';

although:

str[0] = 'b';

would be more natural.

Cheers

Vladimir
 
V

Varun

eliminate
str ='b'
because when we make pointer to string it could be assigned as a
constant
 
E

Emmanuel Delahaye

ramu a écrit :
char *str;

str="hello";
str='b';

Nonsense. I guess you meant

*str = 'b';
What's wrong in this? Am getting segmentation fault.

What about reading the C-FAQ first ? If you don't know where the FAQ is,
Google, knows...
 
E

Emmanuel Delahaye

ramu a écrit :
My actual code is like this:

main()

int main (void)

is C99 compliant.
{
char *str;


str="hai";

*str='b';
printf("%s",str);

}

I want to replace the character "h" with "b". How can I do it?
By using a modifiable object like an array of char :

char str[] = "hai";
*str='b';
 

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

Forum statistics

Threads
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top