简体   繁体   中英

Creating a Calculator using command line interface and malloc,

I am working on malloc and command-line interface where I have to create and simple calculator. I have run the program successfully but I want to print my result from the main, not from the function and I am unable to run it from the main. Every time when I run it will show garbage value. What went wrong?

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

typedef struct {
    int num1;
    int num2;
    uint8_t *ope;
    int result;
} Calc;

void SI( Calc c) {
    if(strcmp(c.ope,"add")==0)
    {
        printf(" result is : %d\n :",c.result = (c.num1 + c.num2 ));
    }
    else if(strcmp(c.ope,"sub")==0)
    {
        printf(" result is :%d\n :",c.result = (c.num1 - c.num2 ));
    }
}

int main(int argc,char *argv[]) {
    Calc *pCalc = (pCalc *) malloc(sizeof(Calc));

    pCalc ->ope = (argv[1]);
    pCalc ->num1   = atoi (argv[2]);
    pCalc ->num2   = atoi (argv[3]);
    SI(*pCalc );

    printf("result is: %d\n", pCalc ->result);  // I want to print result here

    free(pCalc );

    return 0;
}

SI(*pCalc ); passes the value of the structure (essentially a copy) to SI , and void SI( Calc c) declares SI to have a parameter c that is initialized to the passed value. Changes to c in SI affect only the parameter c ; they do not affect the structure in main .

You can change SI(*pCalc ); to SI(pCalc); so that it passes the address of pCalc instead of its value, and you can change the function declaration to void SI(Calc *c) so that its parameter c is a pointer and is initialized to the passed address. Inside SI , change c. to c-> . The . operator accesses a member of a structure, whereas the -> operator accesses a member of a structure that is pointed to.

Alternatively, you could modify the return type of SI so that it returns some result—either the modify structure (return type Calc ) or just the individual result (return type int ). Then you would add a return statement into the function to return the value, and, in the main routine, you could use x = SI(*pCalc); to assign the returned value to x .

You can pass pointer of pCalc to the function SI (as suggested by @EricPostpischil) and set its result there itself.

Also, added some more fixes. Please read the comments // CHANGE HERE .

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

typedef struct
{   int num1;
    int num2;
    uint8_t *ope;
    int result;
} Calc;

// CHANGE HERE - accept pointer argument
void SI(Calc* c) {
    if (c == NULL)
    {
        return;
    }
    // CHANGE HERE - use strncmp instead of strcmp
    if (strncmp(c->ope, "add", 3) == 0)
    {
        c->result = (c->num1 + c->num2);
    }
    else if (strncmp(c->ope, "sub", 3) == 0)
    {
        c->result = (c->num1 - c->num2);
    }
}

int main(int argc,char *argv[]) {
    // CHANGE HERE - command line arguments validation
    if (argc != 4)
    {
        printf("Unexpected number of arguments\n");
        exit(1);
    }

    // CHANGE HERE - pCalc -> Calc
    Calc *pCalc = (Calc *) malloc(sizeof(Calc));

    pCalc ->ope = (argv[1]);
    pCalc ->num1   = atoi (argv[2]);
    pCalc ->num2   = atoi (argv[3]);
    SI(pCalc);  // CHANGE HERE - pass the pointer

    printf("result is: %d\n", pCalc->result);  // I want to print result here

    free(pCalc);

    return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM