我试图确定我的初始化堆栈函数是否获取用户输入的所有值,但是现在我的代码会从我输入的原始值中打印出不同的值。我用了。另外,我正在处理不同的函数来处理堆栈,比如pop,push,到堆栈顶端等等。它会在while循环中工作吗?然而,这里是初始化堆栈功能用多达10个值初始化堆栈
typedef struct stack
{
int* darr;
int size;
int top;
}stack;
stack * initStack(int elements)
{
stack *s;
s = (stack *)malloc(sizeof(stack));
s->darr = (int *)malloc(sizeof(int)*elements);
s->size = 0;
s->top = elements;
return s;
}
主
()这里
int main()
{
stack s;
int i;
printf("Hello user, please enter 10 different values to build your stack: \n");
for(i = 0; i < 10; i++)
{
scanf("%d", initStack(i));
}
printf("\nYou entered: \n%d\n\n", initStack(i));
return 0;
}
点点的东西太多改变有:分配(你分配10层,为空),你scanf函数整数成一个结构,您打印垃圾(因为你分配一个新的堆栈和你返回)和无论如何,你要求printf许多整数,但你通过一个单一的堆栈*值。用stack * pStack = initstack(10)和printf/scanf调用initstack到pStack-> darr ... – 2014-09-10 16:02:31
您对'scanf'和'printf'的使用是不正确的。阅读一两个关于如何使用它们的教程会很有帮助。这是一个初学者。 http://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output – 2014-09-10 16:11:22
在C语言中,不应该使用'malloc' - [我是否使用malloc?](http: //stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – crashmstr 2014-09-10 16:49:42