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.