I have written the next similar example when I get a segmentation fault error
{
const char** var = NULL;
const char* tmp = "Hello! How are you?";
var = (const char**)malloc(5 * sizeof(char*));
for (int i = 0; i < 5; i++)
{
var[i] = (char*)malloc(50* sizeof(char));
strcpy((char*)var[i], tmp);
}
for (int i = 0; var[i]; i++)
{
std::cout << (long int)var[i] << std::endl;
std::cout << var[i] << std::endl;
}
// Free memory
....
}
And on the 6th iteration, for loop doesn't stop (I expected then var[i]==NULL) and I get the "Segmentation fault" error. Can you explain what I am doing wrong, please?
var[6] == NULL?(long int)var[i]to perform a string to integer conversion, or to represent that pointer as a number just to see what it happens to be?