malloc函数!

chengjiawei924 2016-10-21 07:52:16
#include <stdio.h>
#include <stdlib.h>
int stack_create(int **top,int **bottom)
{
int m;
printf("请输入栈的长度:");
scanf("%d",&m);
*bottom=*top=(int *)malloc(sizeof(int)*m);
printf("创建成功!\n");
return m;
}
void stack_in(int *top,int *len,int lenmax)
{
int num;
if((*len)==lenmax)
{
printf("栈已满!无法操作!\n");
return;
}
printf("请输入一个数:");
scanf("%d",&num);
*top=num;
top++;
(*len)++;
return;
}
int stack_out(int *top,int *len)
{
int s;
if(*len==0)
{
printf("栈空!无法操作!\n");
return -999;
}
s=*top;
(*len)--;
top--;
return s;
}
void stack_print(int *bottom,int *len)
{
int i;
if((*len)==0)
{
printf("栈空!无法读取!\n");
return;
}
printf("栈中元素为:\n");
for(i=1;i<=*len-1;i++)
{
printf("%d ",*(bottom+i));
}
putchar('\n');
return;
}
void main()
{
int *bottom,a,*top,lenmax,len=0;
while(1)
{
printf("请输入选择(1:创建一个栈;2:入栈;3:出栈;4:显示栈中已有元素;5:退出):");
scanf("%d",&a);
switch(a)
{
case 1:
{
lenmax=stack_create(&top,&bottom);
break;
}
case 2:
{
stack_in(top,&len,lenmax);
break;
}
case 3:
{
if(stack_out(top,&len)==-999)
{
printf("栈空!无法操作!\n");
}
else
printf("出栈元素为:%d\n",stack_out(top,&len));
break;
}
case 4:
{
stack_print(bottom,&len);
break;
}
case 5:
exit(0);
}
}
}





第一个函数中*bottom=*top=(int *)malloc(sizeof(int)*m);这一句中bottom和top都是指向同一个数组的首地址,而后我让top移动,照理说,bottom还是指向首地址,但是为什么执行过程中bottom指向的地址还是和top一样?
(比如输入的最后一个数是6,执行stack_print后应该输入所有数据,但是输入的是6以及未赋值的数)
...全文
147 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 2016-10-22
  • 打赏
  • 举报
回复
引用 3 楼 chengjiawei924 的回复:
[quote=引用 1 楼 paschen 的回复:] 不懂你要说什么
引用
而后我让top移动
没见malloc后你有让top移动 把程序精简下,问题说清楚
我的目的是创建一个栈顶指针(top)和栈底指针(bottom),创建的时候用malloc函数返回数组的首地址,这个时候top和bottom应该都是首地址,而后我的入栈操作即是让指针top移动,而bottom仍然留在首地址用于打印数组中的元素,可是打印的时候只能打印最后一个元素,我以为这时top和bottom都是指向的同一个地址,而无法实现让bottom留在首地址,而让top移动[/quote] void stack_in(int *top,int *len,int lenmax) int stack_out(int *top,int *len) 第一个参数你应该传递指针的指针,否则传递的top指针只是复制到函数中的,函数中的top并不是外面的top,所以你修改的也只是一个复制品 你的stack_create传递的就是指针的指针,而stack_in stack_out( 也要如此
chengjiawei924 2016-10-22
  • 打赏
  • 举报
回复
引用 2 楼 eastfriendwu 的回复:
void stack_in(int *top,int *len,int lenmax) int stack_out(int *top,int *len) 第一参数都要改:int **top。还有你的lenmax没有初始化吧?
请问为什么第一个参数要改呢?lenmax创建栈的时候就已经返回栈的长度了哟 int stack_create(int **top,int **bottom) { int m; printf("请输入栈的长度:"); scanf("%d",&m); *bottom=*top=(int *)malloc(sizeof(int)*m); printf("创建成功!\n"); return m; } lenmax=stack_create(&top,&bottom);
chengjiawei924 2016-10-22
  • 打赏
  • 举报
回复
引用 1 楼 paschen 的回复:
不懂你要说什么
引用
而后我让top移动
没见malloc后你有让top移动 把程序精简下,问题说清楚
我的目的是创建一个栈顶指针(top)和栈底指针(bottom),创建的时候用malloc函数返回数组的首地址,这个时候top和bottom应该都是首地址,而后我的入栈操作即是让指针top移动,而bottom仍然留在首地址用于打印数组中的元素,可是打印的时候只能打印最后一个元素,我以为这时top和bottom都是指向的同一个地址,而无法实现让bottom留在首地址,而让top移动
eastfriendwu 2016-10-22
  • 打赏
  • 举报
回复
避免二级指针及简化参数,可以试试将栈的数据封装起来。
struct Stack {
stackBase
stackTop
lenMax
}
然后各个函数使用这个结构体指针即可。
eastfriendwu 2016-10-21
  • 打赏
  • 举报
回复
void stack_in(int *top,int *len,int lenmax) int stack_out(int *top,int *len) 第一参数都要改:int **top。还有你的lenmax没有初始化吧?
paschen 2016-10-21
  • 打赏
  • 举报
回复
不懂你要说什么
引用
而后我让top移动
没见malloc后你有让top移动 把程序精简下,问题说清楚

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧