首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

压栈函数——

2012-11-15 
压栈函数——求助!我写了一个压栈函数,貌似不对!头文件没写出来,实际编译时有。int main(int argc, char* arg

压栈函数——求助!
我写了一个压栈函数,貌似不对!头文件没写出来,实际编译时有。
int main(int argc, char* argv[])
{
char numstack[32];
int numtop=-1;
Push(numstack[32], numtop, 'a');
for(int i=0;numstack[i]!='\0';i++)
cout<<numstack[i]<<endl;
printf("Hello World!\n");
return 0;
}

void Push(char stack[], int top, char item)
{
  numtop+=1;
  numstack[numtop]=item;
}


编译之后提示错误:

 error C2664: 'Push' : cannot convert parameter 1 from 'char' to 'char []'
  Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

大神给解释一下,哪里错误?
是不是应该使用指针?!能给出正确的程序就更好了!

[解决办法]

C/C++ code
#include<stdio.h>void Push(char stack[], int *top, char item){  (*top)++;  stack[*top]=item;}int main(){int i;char numstack[32];int numtop=-1;Push(numstack, &numtop, 'a');for(i=0;i<=numtop;i++)printf("%c\n",numstack[i]);printf("Hello World!\n");return 0;}
[解决办法]
1.top改为*top 否则numtop一直等于-1
2.Push(numstack, &numtop, 'a');

热点排行
Bad Request.