How to use argc and argv in a windows terminal?

0

I'm creating a stack for valid expression checking, but it should be used in this int main( int argc, char **argv ) for input. I compiled and did not give any error, however I can not create an executable file through the prompt. And I already forced the creation of one through devc ++, but when doing the test in the terminal the program stops working. I've tried to put the argument in single quotation marks but the program is also killed that way.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define TNOME 100

    struct nodoPilha {
        char nome [TNOME];
        struct nodoPilha *proximoPtr;
    };

    typedef struct nodoPilha NodoPilha;
    typedef NodoPilha *NodoPilhaPtr;

    void imprimePilha( NodoPilhaPtr atualPtr );
    int ehVazia( NodoPilhaPtr headPtr );
    void pop( NodoPilhaPtr *headPtr );
    void push( NodoPilhaPtr *headPtr, char nome[TNOME] );





    int main( int argc, char **argv ){
        NodoPilhaPtr headPtr = NULL;
        int h;
        char *aux;
        int i = 1;
        while (i < argc){
            i++;
            h = ehVazia(headPtr);
            if (h){
                push (&headPtr, argv[i]);           
                aux = argv [i]; 
            }
            else {
                if (aux != argv [i]){
                    pop (&headPtr);
                    char aux;
                }
                else {
                    push (&headPtr, argv [i]);
                }
            }
        }
        if (h){
            printf ("Valida");
        }
        else {
            printf ("Nao Valida");
        }


    }

    void push( NodoPilhaPtr *headPtr, char nome[TNOME] ){
        char * novalinha = strchr(nome, '\n');
        if (novalinha)
            *novalinha = '
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define TNOME 100

    struct nodoPilha {
        char nome [TNOME];
        struct nodoPilha *proximoPtr;
    };

    typedef struct nodoPilha NodoPilha;
    typedef NodoPilha *NodoPilhaPtr;

    void imprimePilha( NodoPilhaPtr atualPtr );
    int ehVazia( NodoPilhaPtr headPtr );
    void pop( NodoPilhaPtr *headPtr );
    void push( NodoPilhaPtr *headPtr, char nome[TNOME] );





    int main( int argc, char **argv ){
        NodoPilhaPtr headPtr = NULL;
        int h;
        char *aux;
        int i = 1;
        while (i < argc){
            i++;
            h = ehVazia(headPtr);
            if (h){
                push (&headPtr, argv[i]);           
                aux = argv [i]; 
            }
            else {
                if (aux != argv [i]){
                    pop (&headPtr);
                    char aux;
                }
                else {
                    push (&headPtr, argv [i]);
                }
            }
        }
        if (h){
            printf ("Valida");
        }
        else {
            printf ("Nao Valida");
        }


    }

    void push( NodoPilhaPtr *headPtr, char nome[TNOME] ){
        char * novalinha = strchr(nome, '\n');
        if (novalinha)
            *novalinha = '%pre%';  

        NodoPilhaPtr newPtr; 
        newPtr=malloc(sizeof(NodoPilha));

        if ( newPtr != NULL ) { 
            newPtr->proximoPtr = *headPtr;
            strcpy( newPtr->nome,nome);

            *headPtr = newPtr;

        } 
        else {
            printf( "%c nao foi inserido. Memoria nao foi disponibilizada.\n", nome );
        } 
    } 
    void pop( NodoPilhaPtr *headPtr ) {
        NodoPilhaPtr tempPtr; 

        tempPtr = *headPtr; 
        *headPtr = ( *headPtr )->proximoPtr;   
        free( tempPtr );
    } 
    int ehVazia( NodoPilhaPtr headPtr ){
        return headPtr == NULL;
    }
    void imprimePilha( NodoPilhaPtr atualPtr ){
        NodoPilhaPtr inicioPtr;
        inicioPtr=atualPtr;
        if ( atualPtr == NULL ) {
            puts( "Pilha esta vazia.\n" );
        } 
        else {
            puts( "A pilha eh:" );
            while ( atualPtr != NULL ) {
                printf( "%s --> ", atualPtr->nome );
                atualPtr = atualPtr->proximoPtr;
            } 
            puts( "NULL\n" );

        } 
    }
'; NodoPilhaPtr newPtr; newPtr=malloc(sizeof(NodoPilha)); if ( newPtr != NULL ) { newPtr->proximoPtr = *headPtr; strcpy( newPtr->nome,nome); *headPtr = newPtr; } else { printf( "%c nao foi inserido. Memoria nao foi disponibilizada.\n", nome ); } } void pop( NodoPilhaPtr *headPtr ) { NodoPilhaPtr tempPtr; tempPtr = *headPtr; *headPtr = ( *headPtr )->proximoPtr; free( tempPtr ); } int ehVazia( NodoPilhaPtr headPtr ){ return headPtr == NULL; } void imprimePilha( NodoPilhaPtr atualPtr ){ NodoPilhaPtr inicioPtr; inicioPtr=atualPtr; if ( atualPtr == NULL ) { puts( "Pilha esta vazia.\n" ); } else { puts( "A pilha eh:" ); while ( atualPtr != NULL ) { printf( "%s --> ", atualPtr->nome ); atualPtr = atualPtr->proximoPtr; } puts( "NULL\n" ); } }
    
asked by anonymous 13.07.2017 / 14:46

2 answers

0

You seem to want to create an arithmetic expression checker, right? The code actually compiles, and compiling generates an executable. The problem is that this executable, when it receives a valid expression like a + b , generates a protection error.

It seems that the error is generated by the increment of the variable i in line 29: when the program enters the loop of line 28, i is already 1, and immediately incrementing, you cause the code to ignore the first argument, causing the program to see only + b ; in addition, the last time it enters the loop , i will be incremented to equal argc and the system will try to stack argv[argc] which, according to the C pattern, is NULL ; when we tried to access it we took the error.

In addition, the code does not actually check at all, it only stacks the first argument, pops it out when it sees the second, it stacks the third, and so on, until it finds the end of the arguments and then returns "Valid" or "Not Valid" according to whether the program received an odd number or pair of arguments.

    
13.07.2017 / 16:45
1

The% argument argument is an integer and has the number of arguments with which the main () function was invoked on the command line.

The argc (arguments argument ) is a vector of strings. Each string of this vector is one of the command line parameters. It is to know how many elements we have in argv we have argv .

Example: The following program makes use of the argv and argc parameters. The program receives the current day, month, and year from the command line, and prints the date in an appropriate format.

See the example, assuming the executable is called argc :

data 26 03 88

The system will have the following output:

26 de Março de 1.988
#include <stdio.h>
int main(int argc, char *argv[])
{
    int mes;
    char *nomemes [] = {"Janeiro", "Fevereiro", "Março", "Abril", "Maio", "Junho",
                        "Julho", "Agosto", "Setembro", "Outubro", "Novembro", "Dezembro"};
    if(argc == 4)
    {
        mes = atoi(argv[2]);

        if (mes<1 || mes>12)
            printf("Erro!\nMes invalido!");
        else
            printf("\n%s de %s de 20%s", argv[1],

        nomemes[mes- 1], argv[3]);
    } else
        printf("Erro!\nUso: data dia mes ano, todos inteiros");
    }

Source: www.univasf.edu.br/~marcelo.linder/arquivos_pc/aulas/aula19.pdf

    
13.07.2017 / 15:05