I'm getting a problem whit malloc()
, don't know what's happening. I'm compiling with MinGW.
Cadena is just a typedef
for char *
, and leer_dato()
is a function that returns a string.
Cadena leer_con_formato(const char * formato)
{
int i = 0;
Cadena dato = leer_dato();
if (strlen(dato) != strlen(formato))
return NULL;
char * nuevo_dato = (char *)malloc(strlen(dato)); // Here's the problem
if (!nuevo_dato)
return NULL;
while (dato[i] != '\0')
{
switch (formato[i])
{
case '0':
if (!isdigit(dato[i]))
return NULL;
nuevo_dato[i] = dato[i];
i++;
break;
case 'C':
if (!isalpha(dato[i]))
return NULL;
nuevo_dato[i] = dato[i];
i++;
break;
case 33 ... 47:
case 58 ... 64:
case 91 ... 96:
case 123 ... 126:
if (!ispunct(dato[i]))
return NULL;
nuevo_dato[i] = dato[i];
i++;
break;
default:
return NULL;
}
}
nuevo_dato[i] = NULO;
return nuevo_dato;
}