I am trying to understand pointers, and trying to define a structure as follows.
| header info (size_t) | pointer to cstring |
here is part of the code. I type defined string as char**.
string str_constructor(const char* const str)
{
size_t* header = (size_t*)(malloc(sizeof(size_t) + sizeof(char**)));
char** data = (char**)(header + 1); //data is now the address of the pointer to cstring
*data = (char*)(malloc(strlen(str) + 1)); // plus one char for null character
for (int i = 0; i != strlen(str); i++)
{
*data[i] = str[i]; // cstring deep copy
}
*header = strlen(str); // store length of string
*data[*header] = '\0'; // append null character
return data;
}
The program crashed on 2nd iteration of the for-loop.
Basically I want to define a string, where *string would return the char*.