[英]Structures and functions in C
I am getting errors in the following code.我在以下代码中遇到错误。 The errors disappear if I take out "struct point p2...".
如果我取出“struct point p2 ...”,错误就会消失。 p1 is assembled the same way and works fine, what is the catch here?
p1 以相同的方式组装并且工作正常,这里有什么问题?
#include <stdio.h>
struct point {
int x;
int y;
};
struct point makepoint(int x, int y)
{
struct point temp;
temp.x = x;
temp.y = y;
return temp;
}
struct point addpoint(struct point p1, struct point p2)
{
p1.x += p2.x;
p1.y += p2.y;
return p1;
}
void main()
{
struct point p1 = makepoint(5, 7);
printf("p1 = (%d, %d)\n", p1.x, p1.y);
struct point p2 = makepoint(2, 9);
printf("p2 = (%d, %d)\n", p2.x, p2.y);
}
#include <stdio.h>
#include <string.h>
struct arithmet {
char eval;
int first_num,sec_num;
};
/* function declaration */
void eval_value( struct arithmet *valyu );
int main( ) {
struct arithmet valyu1;
struct arithmet valyu2;
char oper;
int num1,num2;
oper=getch(); //gets(oper);
scanf("%d",&num1);
scanf("%d",&num1);
valyu1.first_num=num1;
valyu2.sec_num=num2;
valyu1.eval=oper;
eval_value(&num1);
eval_value(&num2);
eval_value(&per);
printBook( &Book1 );
printBook( &Book2 );
return 0;
}
void eval_value(struct arithmet *valyu)
{
valyu->first_num;
valyu->sec_num;
valyu->oper;
if(oper=='+')
{
printf("%d",addit(int a,int b));
}
if(oper=='-')
{
printf("%d",subtractit(int a,int b));
}
if(oper=='*')
{
printf("%d",multiplyit(int a,int b));
}
if(oper=='/')
{
printf("%d",divideit(int a,int b));
}
if(oper=='@')
{
printf("%d",intdivideit(int a,int b));
}
if(oper=='%')
{
printf("%d",remdivideit(int a,int b));
}
if(oper=='~')
{
printf("%d",exponeit(int a,int b));
}
}
#include <stdio.h>
struct arithmetic {
int x;
int y;
char oper;
};
int calcul(int a,int b)
{
struct arithmetic temp;
int x,y;
char proc;
temp.x=x;
temp.y=y;
temp.proc=proc;
if(proc=='+')
{
int addit(int x,int y)
{
return(x+y);
}
}
if(proc=='-')
{
int devit(int x,int y)
{
return(x-y);
}
}
if(proc=='*')
{
int cumlatit(int x,int y)
{
return(x*y);
}
}
if(proc=='/')
{
int dividit(int x,int y)
{
return(x/y);
}
}
if(proc=='@')
{
int intdividit(int x,int y)
{
return(x/y);
}
}
if(proc=='%')
{
int remdivit(int x,int y)
{
return(x%y);
}
}
if(proc=='`')
{
int exponit(int x,int y)
{
int i=1,k=1;
for(i=1;i<=y;i++)
{
k*=x;
}
return(k);
}
}
void main()
{
struct arithmetic v1;
int num1,num2;
char proc;
scanf(&num1);
scanf(&num1);
proc=getch();
v1.x=num1;
v1.y=num2;
v1.oper=proc;
struct calcul();
}
I suggest the following code, which compiles cleanly and exercises each function我建议使用以下代码,它可以干净地编译并练习每个功能
#include <stdio.h>
struct point
{
int x;
int y;
};
void makepoint(int x, int y, struct point* newPoint)
{
newPoint->x = x;
newPoint->y = y;
} // end function: makepoint
void addpoint(struct point* p1, struct point* p2)
{
p1->x += p2->x;
p1->y += p2->y;
} // end function: addpoint
int main()
{
struct point p1;
struct point p2;
makepoint(5, 7, &p1);
printf("p1 = (%d, %d)\n", p1.x, p1.y);
makepoint(2, 9, &p2);
printf("p2 = (%d, %d)\n", p2.x, p2.y);
addpoint( &p1, &p2 );
printf("p1 = (%d, %d)\n", p1.x, p1.y);
return(0);
} // end function: main
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.