3

Can someone please explain to me why this code gives a Segmentation Fault:

char string[] = "this is a string";
char * string2 = "this is another string";
printf("%s\n",string );
printf("%s\n",  string2);
printf("string[2]= %s, string2 = %s\n", string[2], &string2 );

It also gives the same error when I try to print

*string2 or *string2[2] or &string2[2]

I am really confused about this, likewise examples I see on websites seem to print but not this one.

user2847666
  • 119
  • 10
  • You probably want to take a look at this question: [Correct format specifier to print pointer (address)?](http://stackoverflow.com/q/9053658/1298153) – m01 Oct 05 '13 at 09:39

2 Answers2

10

The first two are fine but in the last one you probably want:

printf("string[2]= %c, string2 = %p\n", string[2], (void *)&string2 );
                    ^             ^

You are getting a segmentation fault because you are tricking printf into interpreting a small integer (string[2]) as a pointer (that's what %s expects).

cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • 4
    @zubergu That's what `%p` wants: a `void *`, not just any type of pointer. It probably makes no difference on most machines, but it's good to follow the rules. – cnicutar Oct 04 '13 at 18:17
  • @zubergu No. In first place, because "implicit cast" is an oxymoron, casts are explicit by definition. Second, if you meant "implicit conversion", that doesn't happen either -- `printf()` is not magic. Nor is the compiler. Type conversion rules don't change based on random strings (the `printf()` conversion specifier in this case). –  Oct 04 '13 at 18:21
0
char * string2 = "this is another string";  

declaration causes string2 point to t (first character of string) and that doesn't mean *string2 is entire string (On derefrencing string2),i.e, "this is another string". If you will try to print *string2 with %s, it will cause segmentation fault but with %c it will print t.
To print a pointer use %p specifier.

haccks
  • 104,019
  • 25
  • 176
  • 264
  • By that logic, there is no type in C that can "point to the string". I 'm not sure what your point is. – Jon Oct 04 '13 at 18:15
  • 3
    A "pointer to a string" is by definition a pointer to the string's first character; the term is defined in the C standard, 7.1.1 paragraph 1. (In the absence of that definition, a "pointer to a string" would not necessarily be meaningful, since a "string" is a data layout, not a type.) – Keith Thompson Oct 04 '13 at 18:20
  • 1
    @Jon Apart from Keith's excellent explanation, what if I told you there were pointers-to-array in C? `char (*arrptr)[10] = &some_char_array;` –  Oct 04 '13 at 18:22
  • @H2CO3: You would not be telling me anything new, plus I don't see what that has to do with the declaration "causes `string2` to point to `t`". – Jon Oct 04 '13 at 18:26
  • 1
    @Jon: a string is an array. If you want to "point to a string" instead of just "pointing to the first character of a string", then you use a pointer-to-array, which points to the **entire string,** by definition. (Yeah, I respond to nitpicking with nitpicking.) –  Oct 04 '13 at 18:31
  • 1
    @H2CO3: I 'm sorry to disappoint you, but "A string is a contiguous sequence of characters terminated by and including the first null character", not "an array" as you say -- arrays are something entirely different, plus there are layman's counter-examples that you can think of yourself if you stop nitpicking. – Jon Oct 04 '13 at 18:36
  • 1
    @Jon Um no. Then how do you explain the fact that string literals have type `char [N + 1]` (where `N` is the length of the string)? –  Oct 04 '13 at 18:38
  • @H2CO3: I explain it by saying "string literal != string". If you argue because you believe this answer has merit, please upvote it (though I see you have not done so -- why?). And please stop wasting my time. – Jon Oct 04 '13 at 18:39
  • @Jon "If you argue because you believe this answer has merit, please upvote it" - I don't upvote this answer because it's not correct. However, this answer being incorrect does not in itself mean automatically that you are correct -- you aren't. –  Oct 04 '13 at 18:41