Problem with strcat

1

I have a problem with a program that generates a password, when I debug it comes out that the strcat sentence is skipped and I have no idea why it does that.

void gClaves(tRegLista *reg,int i,int op)
{
int y;


char auxclave[5];
char numclave[4];


switch(op)
{
case 1:
    auxclave[0]='L';
    sprintf(numclave,"%d", reg->contadorLibros);

    if(reg->contadorLibros < 10)
    {
        for(y = 1; y < 4; y++)
        {
            auxclave[y] = '0';
        }
    }
    if(reg->contadorLibros >=10 && reg->contadorLibros < 100)
    {
        for(y = 1; y < 3; y++)
        {
            auxclave[y] = '0';
        }
    }
    if(reg->contadorLibros >=100)
    {
        auxclave[1]= '0';
    }
    //Concateno letra y numeros
    strcat(auxclave, numclave);
    //Copio el resultado de la union dentro de la posicion correspondiente al ejemplar y al campo de clave.
    strcpy(reg->ejemplar[i].idEjemplar, auxclave);
    //FUNCION PARA GENERAR CLAVE EN FUNCION DEL ARTICULO.
    break;
    
asked by Peter 12.04.2017 в 15:47
source

1 answer

2
char auxclave[5];

There you are reserving 5 characters of the stack for a variable called auxclave . At this point the contents of the memory can be any possible combination of 5 characters (for this case and later I will indicate the garbage characters with the% '~' symbol:

auxclave = '~~~~~';

We continue with the execution ...

auxclave[0]='L';

Now you initialize the first element of the arrangement ... the rest still have garbage. The new status of the fix is:

auxclave = 'L~~~~';

We continue ...

if(reg->contadorLibros < 10)
{
    for(y = 1; y < 4; y++)
    {
        auxclave[y] = '0';
    }
}

In the case that there are less than 10 books filled positions 1 to 3 with '0' : auxclave = 'L000~';

if(reg->contadorLibros >=10 && reg->contadorLibros < 100)
{
    for(y = 1; y < 3; y++)
    {
        auxclave[y] = '0';
    }
}

In the case that the number of books is between 1 and 99 ... you write a '0' in bytes 2 and 3 of the array: auxclave = 'L00~~';

if(reg->contadorLibros >=100)
{
    auxclave[1]= '0';
}

instead if the number of books is greater than or equal to 100 then you add a single '0' : auxclave ='L0~~~';

And now comes the error that you detect:

strcat(auxclave, numclave);

strcat tries to add one string to the end of the other. To find the end of the string look for the character ending string 'auxclave' that is in an undetermined place because you have not finished 'sprintf' conveniently.

Solution? Initialize the variable when declaring it:

char auxclave[5] = {0};

In this way all its bytes will be initialized to %code% . Another error that you are going to find is that, since the chain has to store the corresponding finalizing character, the chain must have an extra space reserved for this task. Your arrangement falls short by one character (5 visible characters + finalizer = 6 characters required). The correct thing to do then is to do the following:

char auxclave[6] = {0};

As a bonus, consider making use of if-else statements:

if(reg->contadorLibros < 10)
{
    for(y = 1; y < 4; y++)
    {
        auxclave[y] = '0';
    }
}
else if(reg->contadorLibros < 100) // Si llega a este if es que contadorLibros>9
{
    for(y = 1; y < 3; y++)
    {
        auxclave[y] = '0';
    }
}
else // Te ahorras esta condición al completo
{
    auxclave[1]= '0';
}

Or you can even stay cooler with %code% :

char auxclave[6] = {0};
sprintf(auxclave,"L%04i",reg->contadorLibros);
    
answered by 12.04.2017 / 16:14
source