Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Accessing characters of string (http://www.go4expert.com/forums/accessing-characters-string-t10738/)

FCC 22May2008 23:36

Accessing characters of string
Hi all,

I am brand new to C and I am having so much trouble grasping the basics of it. Consider this code:


char *s = "this is my string";
printf("%s", s[3]);

When I do run this code, I get this error 'Segmentation Fault." I don't really get why...I am just trying to print out like a letter from my string. I read this has to do with trying to access a part of memory that I am not allowed to access? Can anyone explain how to get around this?

shabbir 23May2008 08:33

Re: Accessing characters of string
Just try this

char *s = "this is my string";
char c = s[3];
printf("%s,%c", s, c);

All times are GMT +5.5. The time now is 14:13.