In C99 a string is typically initialized by using the char* data type since there is no primitive "string" data type. This effectively creates an array of chars by storing the address of the first char in the variable:
FILE* out = fopen("out.txt", "w");
char* s = argv[1];
fwrite(s, 12, 1, out);
fclose(out);
//successfully prints out 12 characters from argv[1] as a consecutive string.
How does the compiler know that char* s is a string and not just the address of a singular char? If I use int* it will only allow one int, not an array of them. Why the difference?
My main focus is understanding how pointers, referencing and de-referencing work, but the whole char* keeps messing with my head.
char* sis a string.char* sis a pointer to char, as the same asint* sis a pointer to int. We can get the whole string as long as we reached a final '\0'.int*can be used as an array.i_arrayis a pointer, not an array. The prefix of the[]operator must be a pointer (which is often the result of the implicit conversion of an array). (Actually[]is commutative, but that's beside the point.)