Change a char array within a function

Asked

Viewed 202 times

0

I am trying to pass a char array pointer to a function. I want it to make changes to this char array.

Follow the function code:

void dhnfe( char tzd, char hverao, char *dh)
{
    if (hverao == 1)
    {

        switdh(tzd)
        {
        case -2: /*Fuso de Fernando de Noronha */
            dh = "%Y-%m-%d-T-%H:%M:%S-2:00";
            break;
        case -3: /*Fuso de Brasilia*/
        default:
            dh = "%Y-%m-%d-T-%H:%M:%S-3:00";
            break;
        case -4: /*Fuso de manaus*/
            dh = "%Y-%m-%d-T-%H:%M:%S-4:00";
            break;
        }
    }
    else if (hverao == 0)
    {
        switdh(tzd)
        {
        case -2: /*Fuso de Fernando de Noronha Horario de Verao*/
            dh = "%Y-%m-%d-T-%H:%M:%S-1:00";
            break;
        case -3: /*Fuso de Brasilia Horario de Verao*/
        default:
            dh = "%Y-%m-%d-T-%H:%M:%S-2:00";
            break;
        case -4: /*Fuso de Manaus Hoario de Verao*/
            dh = "%Y-%m-%d-T-%H:%M:%S-3:00";
            break;
        }
    }
}
  • The types of the parameters would not be signed char and char?

  • 1

    What is dhar??

  • Actually it was a typo, they’re "char"

1 answer

0

What I wanted was to change the string dh which passed as a parameter in the function. At the time I passed as *dh instead of **dh/ *dh[].

This code changes the string in two different ways (for teaching purposes), by return and within the function.

  static char  *dhnfe(signed char tzd,signed char hverao,char **dh){
char *aux;

if (hverao == 1){

    switch (tzd){
        case -2: /*Fuso de Fernando de Noronha */
            aux = "%Y-%m-%d-T-%H:%M:%S-2:00";
            break;
        case -3:/*Fuso de Brasilia*/
        default:        
            aux = "%Y-%m-%d-T-%H:%M:%S-3:00";
            break;
        case -4:/*Fuso de manaus*/
            aux = "%Y-%m-%d-T-%H:%M:%S-4:00";
            break;
    }
}else if(hverao == 0){
    switch (tzd){
        case -2:/*Fuso de Fernando de Noronha Horario de Verao*/
            aux = "%Y-%m-%d-T-%H:%M:%S-1:00";
            break;
        case -3:/*Fuso de Brasilia Horario de Verao*/
        default:        
            aux = "%Y-%m-%d-T-%H:%M:%S-2:00";
            break;
        case -4:/*Fuso de Manaus Hoario de Verao*/
            aux = "%Y-%m-%d-T-%H:%M:%S-3:00";
            break;
    }
}
*dh = aux;
return aux;

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.