I have written a function that parses a url and splits it into it’s components. To store the components of the URL, I pass a pointer (to the function) to the following structure called urlinfo:
typedef struct urlstruct {
char** protocol;
char** address;
char** port;
char** page;
} urlstruct;
The reason for it containing double pointers is because I don’t know how long each components of the URL will be. Inside the function, I work out the size needed to store each component and attempt to assign the memory to the components of the urlstruct using the following line:
*(urlinfo->protocol) = (char*)malloc(i * sizeof(char));
where (i * sizeof(char)) is the size required. This line causes an access violation. Does anyone know how to correctly allocate the memory?
Thanks in advance 🙂
Assuming you’re really using C++, you should instead just have:
Then filling it becomes easy:
And you don’t have to worry about anything else.
But, if your tag is wrong and you really need cstrings for some reason:
You only need single pointers. You can then allocate memory for them in very close to the same way you already are: