0

I'm a beginner at learning c. But I don't know the whether the str is a pointer variable or an array. If str is a pointer variable then why does the first printf works? It prints out the hole string ''Information'', and the second printed out ''ation''.

#include <stdio.h>
int main ()
{
    char *str = "Information";
    printf("%s\n", str);
    printf("%s\n", str+6);
    printf("%c\n", *(str+6));
    printf("%c\n", str[6]);

return 0;
}

Here is what I thought the program will write like:

#include <stdio.h>
int main ()
{
    char str[15] = "Information";
    char *a = str;
    printf("%s\n", str);
    printf("%s\n", str+6);
    printf("%c\n", *(a+6));
    printf("%c\n", str[6]);

return 0;
}
Steven499
  • 1
  • 2
  • 4
    Arrays are automatically converted to a pointer to the first element when used as an R-value. So when calling a function, the two methods are equivalent. – Barmar Sep 10 '21 at 15:41
  • The second output line of the first program surely was "ation"? – Peter - Reinstate Monica Sep 10 '21 at 15:44
  • The biggest difference there is that you WON'T be able to edit the string (in place) in the first example, because there, `str` is pointing to a constant. In the second example, you can edit either the values in the array directly, or through that pointer. – yhyrcanus Sep 10 '21 at 15:49
  • `str[6]` is equivalent to `*(str+6)`; indeed, it is *defined* to be equivalent, which is why you can write `6[str]`: It is `*(6+str)`. Because indexing is defined in terms of addition, and addition is a symmetrical operation (you can swap the two operands and get the same result), *indexing is a symmetrical operation*, even though it is a very asymmetrical notation. – Peter - Reinstate Monica Sep 10 '21 at 15:50
  • 1
    The question was close too quickly so I couldn't post an answer, but I think you're probably confused about the difference between string literals and arrays, I'd suggest reading up on that. The difference is basically just where they're stored in memory. In your first example the pointer points to a string literal which cannot be modified (I don't want to say no memory is allocated because that's not accurate but it's similar). The second case allocates an array initializes it. A char* can point to either and they behave the same except for attempts to modify. – LEF Sep 10 '21 at 15:52
  • Another hint: Omit the array length and write `char str[] = "Information";`. That way the compiler counts for you. – Peter - Reinstate Monica Sep 10 '21 at 15:53
  • Thank you guys for the reply! I really appreciate that. – Steven499 Sep 10 '21 at 16:02
  • 1
    @LEF duplicates are to avoid not needed duplicate answers. The issue you mention was answered many times in the past: https://stackoverflow.com/questions/12795850/string-literals-pointer-vs-char-array – 0___________ Sep 10 '21 at 16:16
  • 2
    @LEF There is no "difference between string literals and arrays" -- string literals *are* arrays. One can be confused about arrays and pointers though, and the first one who is confused is the language ;-). – Peter - Reinstate Monica Sep 10 '21 at 16:37
  • @Reinstate Monica "and the first one who is confused is the language" -- So true! – Zakk Sep 10 '21 at 17:14
  • @Peter-ReinstateMonica: There *is* a reason for the decay rule; it allowed Ritchie to keep B's array semantics without having to set aside storage for the pointer those semantics required. However, it means that array expressions lose their "array-ness" under most circumstances. – John Bode Sep 10 '21 at 17:31

0 Answers0