determining a string inside a switch in C

2

I have doubts on how to define a string inside a switch in C, without having to do a table of constants for this.

The variable name_lanche is of type char, but this is the number of characters defined, so I am using it as a string. I need the variable to be empty before the switch and after passing the switch if the case is true it would get the designated value.

    
asked by anonymous 07.09.2016 / 22:56

2 answers

2

There are 3 ways to solve the problem without using a constant table.

1) Char pointer to string constant:

#include <stdlib.h>
#include <stdio.h>

int main( int argc, char * argv[] )
{
    char * nome_lanche = NULL;
    int preco_lanche = 0;
    int cod_lanche = atoi( argv[1] );

    switch( cod_lanche )
    {
        case 0:
            nome_lanche = "macarrao";
            preco_lanche = 10;
            break;

        case 100:
            nome_lanche = "hamburger";
            preco_lanche = 5;
            break;

        default:
            nome_lanche = "invalido";
            preco_lanche = -1;
            break;
    }

    printf("Codigo: %d\n", cod_lanche );
    printf("Nome: %s\n", nome_lanche );
    printf("Preco: %d\n", preco_lanche );

    return 0;
}

/* fim-de-arquivo */

2) Static memory allocation (chars vector):

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define NOME_MAX_TAM  (100)

int main( int argc, char * argv[] )
{
    char nome_lanche[ NOME_MAX_TAM + 1 ] = {0};
    int preco_lanche = 0;
    int cod_lanche = atoi(argv[1]);

    switch( cod_lanche )
    {
        case 0:
            strcpy( nome_lanche, "macarrao" );
            preco_lanche = 10;
            break;

        case 100:
            strcpy( nome_lanche, "hamburger");
            preco_lanche = 5;
            break;

        default:
            strcpy( nome_lanche, "codigo invalido" );
            preco_lanche = -1;
            break;
    }

    printf("Codigo: %d\n", cod_lanche );
    printf("Nome: %s\n", nome_lanche );
    printf("Preco: %d\n", preco_lanche );

    return 0;
}

/* fim-de-arquivo */

3) Dynamic memory allocation:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( int argc, char * argv[] )
{
    char * nome_lanche = NULL;
    int preco_lanche = 0;
    int cod_lanche = atoi(argv[1]);

    switch( cod_lanche )
    {
        case 0:
            nome_lanche = strdup("macarrao");
            preco_lanche = 10;
            break;

        case 100:
            nome_lanche = strdup("hamburger");
            preco_lanche = 5;
            break;

        default:
            nome_lanche = strdup("codigo invalido");
            preco_lanche = -1;
            break;
    }

    printf("Codigo: %d\n", cod_lanche );
    printf("Nome: %s\n", nome_lanche );
    printf("Preco: %d\n", preco_lanche );

    free( nome_lanche );

    return 0;
}

/* fim-de-arquivo */

I hope I have helped!

    
08.09.2016 / 02:18
1

Not possible. Or you should use other language constructs ( if strcmp() ) or create an auxiliary table to handle this (a hash table is often used).

If you can use constants in place of strings , it is better.

    
07.09.2016 / 23:06