> And now you have a "string pointer" which is distinct from a "data pointer". You can't allocate a heap block and "put a string in it" because the special thing at the start needs to go with the pointer and not the data in the block.
The second sentence doesn't follow from the first one.
struct string {
char (*ptr)[static len];
size_t len;
};
struct string new_copy = { .ptr = malloc(100), .len = 100; };
new_copy = strcpy(new_copy, "some other string"); // string literal is a syntactic sugar for statically allocated 'struct string' with 'ptr' set to an unnamed, statically allocated char[] buffer inside it, and proper 'len' field.
struct string strcpy(struct string dest, struct string src) {
struct string result = { .ptr = dest.ptr, .len = min(dest.len, src.len) };
memcpy(result.ptr, src.ptr, result.len);
return result;
}
Or you can do
struct string *new_copy = malloc(100 + sizeof(struct string));
new_copy->ptr = (char*)(new_copy + 1);
new_copy->len = 100;
if you really want to. Just don't pass naked "char*" around, that thing has no obvious extent.