Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > A 'char * string' assigning problem

Reply
Thread Tools

A 'char * string' assigning problem

 
 
lili
Guest
Posts: n/a
 
      07-17-2009
Please forgive my poor english.The following is my code:
-------*************************************************---------
#include <stdio.h>
#include <string.h>

int main()
{
char* teststr = "hello";
char* yes = teststr;
*(++yes) = 'w';
printf(yes);
}
-------*************************************************---------
print: segment error
Why did i got a 'segment error'?
but
-------*************************************************---------
#include <stdio.h>
#include <string.h>

int main()
{
char teststr[20] = "hello";
char* yes = teststr;
*(++yes) = 'w';
printf(yes);
}
-------*************************************************---------
print: hwllo
or
-------*************************************************---------
#include <stdio.h>
#include <string.h>

int main()
{
char* teststr = "hello";
char* yes = teststr;
printf("%c\n", *(++yes));
}
-------*************************************************---------
print: e

is no problem.
Can anyone help me , Thank you!
 
Reply With Quote
 
 
 
 
Ben Bacarisse
Guest
Posts: n/a
 
      07-17-2009
lili <(E-Mail Removed)> writes:

> Please forgive my poor english.The following is my code:
> -------*************************************************---------
> #include <stdio.h>
> #include <string.h>
>
> int main()
> {
> char* teststr = "hello";


The arrays that result from a string literal are unmodifiable. The
effect of doing so is undefined.

> char* yes = teststr;
> *(++yes) = 'w';


'yes' is the same pointer as 'teststr' and 'teststr' points at an
unmodifiable array of characters. Trying to put that 'w' where the
'e' is in the original string causes anything to happen. A segfault
one of the most helpful outcomes.

Most people prefer to write:

const char *teststr = "hello";

so as to be reminded when an attempt is made to modify the target
string.

> printf(yes);
> }
> -------*************************************************---------
> print: segment error
> Why did i got a 'segment error'?
> but
> -------*************************************************---------
> #include <stdio.h>
> #include <string.h>
>
> int main()
> {
> char teststr[20] = "hello";


teststr is now a plain local array whose elements get initialised
using the string hello. The chars in teststr can be modified with no
ill effects.

> char* yes = teststr;
> *(++yes) = 'w';
> printf(yes);
> }
> -------*************************************************---------
> print: hwllo
> or
> -------*************************************************---------
> #include <stdio.h>
> #include <string.h>
>
> int main()
> {
> char* teststr = "hello";
> char* yes = teststr;
> printf("%c\n", *(++yes));


And here there is no modification to cause a problem.

> }
> -------*************************************************---------
> print: e
>
> is no problem.
> Can anyone help me , Thank you!


--
Ben.
 
Reply With Quote
 
 
 
 
lili
Guest
Posts: n/a
 
      07-17-2009
Thank you Ben.I see
 
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
Problem with assigning a value to a array atk0309 C++ 3 11-13-2006 10:11 AM
Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency? weston Javascript 1 09-22-2006 09:33 AM
Problem assigning selectedItem.Value to a dropdownlist filled with OnItemDataBound Antonio D'Ottavio ASP .Net 1 09-19-2005 09:41 AM
problem with assigning data from data reader to label control in web form mhnazly ASP .Net 1 10-28-2003 12:19 AM



Advertisments