[英]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.我正在研究 malloc 和命令行界面,我必须在其中创建简单的计算器。 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.将结构的值(本质上是一个副本)传递给SI
,并且void SI( Calc c)
声明SI
具有初始化为传递值的参数c
。 Changes to c
in SI
affect only the parameter c
; SI
对c
更改仅影响参数c
; they do not affect the structure in main
.它们不影响main
的结构。
You can change SI(*pCalc );
您可以更改SI(*pCalc );
to SI(pCalc);
到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.以便它传递pCalc
的地址而不是它的值,并且您可以将函数声明更改为void SI(Calc *c)
以便其参数c
是一个指针并被初始化为传递的地址。 Inside SI
, change c.
在SI
内部,更改c.
to c->
.到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
).或者,您可以修改SI
的返回类型,使其返回一些结果——修改结构(返回类型Calc
)或仅返回单个结果(返回类型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);
然后,你还要加上return
语句转换成函数返回值,并且,在main
例程中,你可以使用x = SI(*pCalc);
to assign the returned value to x
.将返回值赋给x
。
You can pass pointer of pCalc
to the function SI
(as suggested by @EricPostpischil) and set its result
there itself.您可以将pCalc
指针pCalc
给函数SI
(如@EricPostpischil 所建议的)并将其result
设置在那里。
Also, added some more fixes.此外,添加了更多修复。 Please read the comments // CHANGE HERE
.请阅读评论// 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.