bus errors accessing a char * by index?!

C

crrrystal

i can't seem to figure out why this code refuses to work:

char *s = "hello";
printf("%s", s[0]);

i keep getting bus errors when i run this. whyyyyyy?

i also get the same type of errors when trying to do strncpy and such.

please help.
 
K

Kenny McCormack

i can't seem to figure out why this code refuses to work:

char *s = "hello";
printf("%s", s[0]);

i keep getting bus errors when i run this. whyyyyyy?

i also get the same type of errors when trying to do strncpy and such.

please help.

Sounds like a defective RAM chip. You need to buy a new computer.
 
A

Artie Gold

i can't seem to figure out why this code refuses to work:

char *s = "hello";
printf("%s", s[0]);

You are using the %s format specifier, which is used to print a C
string, i.e. a pointer to a null terminated sequence of chars.

s[0], in your example, is a char.

See the problem?

[hint: what does the %c format specifier do?]
i keep getting bus errors when i run this. whyyyyyy?

i also get the same type of errors when trying to do strncpy and such.

please help.
HTH,
--ag
 
M

Michael

char *s = "hello";
printf("%s", s[0]);

i keep getting bus errors when i run this. whyyyyyy?

You probably want to do either:
printf("%s", s); /* print the whole string */
or
printf("%s", &s[0]); /* print the whole string; another way to say
the same thing */
or
printf("%c", s[0]); /* print just the first character. */

Something along the following lines is happening (although this is
implementation defined, so it may not be exactly this):

Let's say s is stored at memory location 0x123456
My version 1 says 'print the string starting at memory location
0x123456'
My version 2 says 'print the string starting at the memory location
that is the address of the first character of s' (which is, in turn,
0x123456, which is fine).
My version 3 says 'print the character s[0]' which is 'h' which is
ascii 0x68.
Now here's the kicker:
Your version says 'print the string starting at memory location
0x68.' But who knows what is at 0x68? Probably garbage. Or worse.

Michael
 
C

crrrystal

ahh.. okay i see. now why can't i write to:

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

?

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"
 
K

Keith Thompson

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.
 
M

Mark McIntyre

ahh.. okay i see. now why can't i write to:

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

This is a FAQ - 16.8.
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";

once you've figured out why you can't modify this, you will be able to
solve your problem using an array of chars.
by finding the index of the / , set s[4]='\0', so now i have two
strings: s is "dirA" and (s+4) is "dirB"
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 
C

CBFalconer

.... snip ...

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"

Just look up strtok(). All done.

--
Some informative links:
http://www.geocities.com/nnqweb/
http://www.catb.org/~esr/faqs/smart-questions.html
http://www.caliburn.nl/topposting.html
http://www.netmeister.org/news/learn2quote.html
 
J

Joe Estock

CBFalconer said:
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"

Just look up strtok(). All done.

strtok() wouldn't help in this case. The same issue would still be there
(bus error in OP's case, segmentation violation in my experience). In
short you cannot modify a string literal and it's important for the OP
to know this distinction.

Below is the strtok() route and below that is the method you previously
mentioned. Either one will work. The second one is probably more complex
than needed, but it gets the job done.

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

int main(void)
{
char s[] = "dirA/dirB";
char *p = NULL;
int i = 0;

p = strtok(s, "/");

if (p == NULL)
{
printf("Whoops! strtok() returned NULL\n";
return(0);
}

while(p != NULL)
{
printf("element %d: \"%s\"\n", i, p);
p = strtok(NULL, "/");
}

return(0);
}

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

int main(void)
{
char s[] = "dirA/dirB";
int i = 0;
int j = 0;
int totElem = 1;
int len = strlen(s);
char *p = NULL;

for (i = 0; i < len; i++)
{
if (s == '/')
{
s = '\0';
totElem++;
}
}

p = s;
for (i = 0; i < totElem; i++)
{
printf("element %d: \"%s\"\n", i, p);

for (j = 0; p[j] != '\0'; j++) ;
p += j + 1;
}

return(0);
}
 
J

Joe Estock

Joe said:
CBFalconer said:
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"

Just look up strtok(). All done.

strtok() wouldn't help in this case. The same issue would still be there
(bus error in OP's case, segmentation violation in my experience). In
short you cannot modify a string literal and it's important for the OP
to know this distinction.

Below is the strtok() route and below that is the method you previously
mentioned. Either one will work. The second one is probably more complex
than needed, but it gets the job done.

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

int main(void)
{
char s[] = "dirA/dirB";
char *p = NULL;
int i = 0;

p = strtok(s, "/");

if (p == NULL)
{
printf("Whoops! strtok() returned NULL\n";
return(0);
}

while(p != NULL)
{
printf("element %d: \"%s\"\n", i, p);
p = strtok(NULL, "/");

i++;

/* Might help to increment the counter variable. */
}

return(0);
}

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

int main(void)
{
char s[] = "dirA/dirB";
int i = 0;
int j = 0;
int totElem = 1;
int len = strlen(s);
char *p = NULL;

for (i = 0; i < len; i++)
{
if (s == '/')
{
s = '\0';
totElem++;
}
}

p = s;
for (i = 0; i < totElem; i++)
{
printf("element %d: \"%s\"\n", i, p);

for (j = 0; p[j] != '\0'; j++) ;
p += j + 1;
}

return(0);
}
 
C

crrrystal

Thanks everyone! I eventually got this segment of my program to work..
although not as concisely as I would've hoped. If I get the rest of
this done early then I'll try a second stab at it.
 
R

Richard Heathfield

Kenny McCormack said:
i can't seem to figure out why this code refuses to work:

char *s = "hello";
printf("%s", s[0]);

i keep getting bus errors when i run this. whyyyyyy?

i also get the same type of errors when trying to do strncpy and such.

please help.

Sounds like a defective RAM chip. You need to buy a new computer.

If you must play your silly little games, try to play them in a way that
doesn't cost people real money.

To the OP: the code is flawed, as you rightly guessed. There is no reason to
think that any RAM chips are defective. McCormack is simply being
McCormack.
 

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
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top