C 语言程序,用于查找堆栈的增长方向


堆栈是一种用来存储元素的数据结构。堆栈上有两个操作。push用于将新元素添加到堆栈中。pop用于从堆栈中移除一个元素。

堆栈可以根据使用它的程序的性质向上和向下增长。该程序用于在程序中查找堆栈的增长方向。

算法

Step 1: Create a local variable in the main function.
Step 2: Create a function that with a local variable.
Step 3: Call the function from the main function. And then compare the local variables of in both these functions.
Step 4: Compare the address of local variables in main and the function.
Step 5: If address variable in main is more than local variable of the function, then stack grows upward otherwise it grows downward.

示例

 运行演示

#include<stdio.h>
void fun(int *main_local_addr){
   int fun_local;
   if (main_local_addr < &fun_local)
      printf("Stack grows upward
");    else       printf("Stack grows downward
"); } int main(){    int main_local;    fun(&main_local);    return 0; }

输出

Stack grows downward

更新时间: 2019 年 9 月 19 日

533 次浏览

启动你的 事业

完成课程并获得认证

立即开始
广告
© . All rights reserved.