Re: bus errors accessing a char * by index?!



crrrystal@xxxxxxxxx writes:
ahh.. okay i see.

You see what? Please provide context when posting a followup.

Suggested links:

http://cfaj.freeshell.org/google/
http://www.caliburn.nl/topposting.html
http://clc-wiki.net/wiki/Introduction_to_comp.lang.c

It would also be helpful if you would use standard capitalization; by
making it easier it is for us to read what you write, you can help us
to help you.

now why can't i write to:

char *s = "hello";
s[3]='\0'; <-causes the error
printf("%s", s);

?

What exactly do you mean by "causes the error"? The problem in this
case happens to be fairly obvious, but in general it's very important
to tell us *what* went wrong. You might also want to read
<http://www.catb.org/~esr/faqs/smart-questions.html>.

The problem is that you're trying to modify a string literal. A
string literal such as "hello" specifies a statically allocated array
of characters. Any attempt to modify this array, as you've done
above, invokes undefined behavior, which means it may or may not work,
and the compiler isn't obligated to tell you that you've made a
mistake. Don't do that.

If you want a string you can modify, you can declare it like this:

char s[] = "hello";

Here you declare an array (which you can modify), and the string
literal specifies how the array is initialized. This is in contrast
to your original declaration, in which the *pointer* is initialized to
point to the (possibly unmodifiable) array associated with the string
literal itself.

basically, the bigger picture what i'm trying to is extract words from
a string with / as its delimiter , such as

char * s = "dirA/dirB";

by finding the index of the / , set s[4]='\0', so now i have two
strings: s is "dirA" and (s+4) is "dirB"

You might want to look at the standard strtok() function. It has some
serious problems (it modifies the string you're splitting, it can't be
used on more than one string at a time, and its treatment of
consecutive delimiters is questionable), but it may be just what
you're looking for.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: string upper, string lower, string subthisforthat
    ... | allowed to modify any elements of the input elements. ... | sequence operations section of the Standard. ... | that the sequence is not allowed to be modified. ... | error to leave out case insensitive string comparisons. ...
    (alt.comp.lang.learn.c-cpp)
  • Help in French|Spanish|German translation.
    ... I am also an author of User-defined string functions. ... WORDTRANEX (cSearched, cArExpressionSought | cExpressionSough, ... each string of the array is searched ... If the parameter nArStartOccurrence is -1 or omitted, the replacement starts ...
    (microsoft.public.fox.helpwanted)
  • Re: passing a string to a dll
    ... Joe, I really appreciate you taking the time to demonstrate this. ... sure how I would implement indexing it for random alphanumeric codes. ... I might handle the array. ... I actually have been wondering if I could use a second string ...
    (microsoft.public.vc.mfc)
  • Re: passing a string to a dll
    ... I might handle the array. ... I actually have been wondering if I could use a second string ... look at insertion cost, organization cost, and search cost. ...
    (microsoft.public.vc.mfc)
  • Re: Array Type Mismatch
    ... Dim resultAs String ... I am still somewhat confused as to why nothing is stored in the array. ... Public Function TextBoxGetLine(....arguments... ...
    (microsoft.public.access.formscoding)