Char * an array of characters, but int * not array of ints?

In C99, a string is usually initialized using the char* data type, since there is no primitive data type of the type. This effectively creates an array of characters, storing the address of the char variable 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, not just the singular address of char ? If I use int* , it will only allow one int , not an array of them. Why is the difference?

My main focus is understanding how pointers, links, and dereferences work, but the whole char* keeps fiddling with my head.

+6
source share
3 answers

How does the compiler know that char* s is a string, and not just the address of a special char?

This is not true. As for the β€œcompiler,” char* s is a pointer to a char.

On the other hand, there are many library functions that assume that a char* points to an element of the char zero sequence (see, for example, strlen , strcmp , etc.).

Note that fwrite does not make this assumption. This requires you to tell how many bytes you want to write (and that this number will not take you outside the buffer pointed to by the first argument.)

If I use int* , it will allow only one int, not an array of them. Why is the difference?

This is not true. In C, there is no special case for char* . int* can also point to an element of an int array. In fact, you could write a library that uses 0 or another sentinel value to indicate the end of an int sequence, and use it a lot in the same as char* , used by convention.

+15
source

In your code

 fwrite(s, 12, 1, out); 

equivalent to writing

write 12 elements with a size of 1 byte, the location, starting from the address s , to the file indicated by out .

Here. a char has one byte exactly, so you get the desired result.

How does the compiler know that char* s is a string, not just the address of a special char ?

Well, this is not so (and not necessary). You asked (read from s and) write 12 bytes for him to do this. If memory is not available, this is a programming error. fwrite() alone cannot handle it.

Caution

  • If s no memory allocated to access s[11] (technically), this will be undefined behavior . It is up to the programmer to pass real values ​​as an argument.

In the case of int size is 4 bytes (as a rule, in a 32-bit system), and byte bytes will not give the desired result.

In this case, you need to use fprintf() to print the formatted output.

+7
source

The compiler will have no idea, except char * - the address of the character. We can get him to read the characters following by incrementing the address of the first character. The case is similar to any pointer int *, long *, etc. The compiler simply processes the pointer, because something points to an address of its type.

0
source

Source: https://habr.com/ru/post/990067/


All Articles