对不起,我是新手。继续我的函数原型和调用问题。我认为我的函数调用或原型是不正确的。我只想将box.item和box.bin的值传递给Stackers。
实际上我想在这里做的是两个有两个Stack,一个是Stacker和Truck。
两者都使用Stack,void truck(){}
使用动态内存分配。并且void stacker(){}
固定大小为5.我已根据我想要的内容绘制图像:
#include <stdlib.h>
#include <stdio.h>
struct box
{
int item;
int bin;
int top;
}*boxs,stacker[5];
void stackers(struct box *);
int i;
int num,a;
int topt = -1;
int tops = -1;
void truck();
void display();
void display()
{
for(i=0;i<=topt;i++)
{
printf("\n%d",boxs[i].item);
printf("\n%d",boxs[i].bin);
printf("\nstacker%d",stacker[i].item);
printf("\nstacker%d",stacker[i].bin);
}
}
void main()
{
int op;//option
char ch;//choice
do{
system("cls");
printf("\n Stack:-");
printf("\n\n 1.<Insert>");
printf("\n 2.<Display>");
printf("\n\n Option: ");
scanf("%d", &op);fflush(stdin);
switch(op)
{
case 1:truck();break;
case 2:display();break;
default:printf("\n Invalid Choice!!!");break;
} printf("\n\n\n Continue?(Y/N): ");
scanf("%c", &ch); fflush(stdin);
}while(ch=='y' || ch=='Y');
return;
}
void truck()
{
struct box e;
int temp;
printf("Enter Number of Boxes to unload from the Truck: ");
scanf("%d", &num);
boxs= (struct box*)malloc(num*sizeof (struct box));
temp=num;
/*tempt->tops=copy-1;*/
for(a=0; a<num;a++)
{
topt++;
temp--;
/*tempt[copy-a].tops--;*/
printf("\n1. Enter Item ID: ");
scanf("%d", &e.item);
boxs[a].item=e.item;fflush(stdin);
/*tempt[copy-a].item=e.item;*/
printf("\n2. Enter Bin ID: ");
scanf("%d", &e.bin);
boxs[a].bin=e.bin;fflush(stdin);
//tempt[copy-a].bin=e.bin;
stackers(boxs);
}
}
void stackers(struct box * pass)
{
if(tops==4)
{
}
else{
tops++;
stacker[tops].item=pass->item;
stacker[tops].bin=pass->bin;
}
}
答案 0 :(得分:2)
更改
void stackers(struct box boxs);
...
stackers(&boxs); // call
到
void stackers(struct box *);
...
stackers(boxs); // call
已更新:对于新的更新代码,仍有几个问题:
您没有包含所需的标题:
#include <stdlib.h>
#include <stdio.h>
将函数声明放在main()
之前,而不是放在其中:
void truck();
void display();
void main()
{
您没有定义函数display()
。
答案 1 :(得分:0)
有两种方法可以做到这一点。
堆栈:
struct box mybox;
stackers(&mybox); // call by address on the stack
堆:
struct box * mybox_pointer = calloc(sizeof(struct box), 1);
stackers(mybox_pointer); // call by pointer
答案 2 :(得分:0)
这是另一种方式......
注意: 使用 阅读(希望有用的)解释的内联评论...... typedef
。这允许您使用typedef'd name #include <stdio.h>
#include <stdlib.h>
typedef struct
{
int item;
int bin;
int top;
}BOX; //BOX is now a type, and can be used to create instances of your struct.
BOX stacker[5], *boxs;//create array of 5, and pointer to an instance of the struct.
void stackers(BOX *a);//prototype of your function
//stackers(&boxs); // call (you cannot call a function outside of a block, {...})
int main(void)//must include a main function (for any C program)
{ //program starts when operating system calls this function
boxs = &stacker[0]; //initialize pointer to BOX like this
stackers(boxs); // call it here (note, no & needed)
return 0;
}
void stackers(BOX *pass)
{
if(pass[0].top==4)
{
}
else{//not sure what you are doing here, changes simply reflect what needed to be done for a compile build
pass[0].top++;
stacker[pass[0].top].item=pass[0].item;
stacker[pass[0].top].bin=pass[0].bin;
}
}