I'm new to the C programming language, and I was under the impression that strings are just arrays of characters. However, when I tried the following code below (among some other tests):
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char apple1[] = { 'a', 'p', 'p', 'l', 'e', '\0' };
char *apple2 = "apple";
char apple3[] = "apple";
printf("%i\n", apple1 == apple2); // 0
printf("%i\n", apple2 == apple3); // 0
printf("%i\n", apple3 == apple1); // 0
printf("%i\n", "apple" == apple1); // 0
printf("%i\n", "apple" == apple2); // 1
printf("%i\n", "apple" == apple3); // 0
printf("%i\n", !strcmp(apple1, apple2)); // 1
for (size_t i = 0; i < strlen(apple) + 1; i++)
{
printf("%i", apple1[i] == apple2[i]);
} // 111111
return 0;
}
I got some unexpected results. Is there any reason for these, at least for me, counterintuitive results? Thank you very much.