How to use argc and argv on a windows terminal?

Asked

Viewed 560 times

0

I am creating a stack for valid expression check, but it should be used in this int main( int argc, char **argv ) for input. I compiled and did not make a mistake, but I cannot create an executable file through the prompt. And I’ve already forced the creation of one through devc++, but when I do the terminal test the program stops working. I’ve tried to put the argument in simple quotes 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 = '\0';  

        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" );

        } 
    }

2 answers

1

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

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

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

See the example, assuming the executable is called data:

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

0


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

It seems to me that the error is generated by incrementing the variable i on line 29: when the programme enters the loop of line 28, i already is 1, and immediately incrementing, you make the code ignore the first argument, causing the program to see only + b; Besides, the last time he entered the loop, the i shall be incremented to equal argc and the system will try to stack argv[argc] which, according to standard C, is NULL; when we try to access it we make the mistake.

Also, the code doesn’t actually do any checking yet, just pushes the first argument, pops it when you see the second, pushes the third, and so on, until you find the end of the arguments and then returns "Valid" or "Not Valid" according to whether the program received an odd number or even of arguments.

  • The problem really was on line 28, the code I finished and made the modification.. obg

Browser other questions tagged

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