码迷,mamicode.com
首页 > 其他好文 > 详细

在堆内存中存放十个字符串并输出 堆栈溢出时退出程序

时间:2019-10-04 23:09:33      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:数组   字符   enter   div   退出   def   turn   ring   col   

 1 # include <stdio.h>
 2 # include <stdlib.h>
 3 
 4 # define NUM 10
 5 
 6 int main()
 7 {
 8     char *str[NUM];  /* 定义一个字符性的指针数组 */
 9     int t;
10 
11     /* 为数组中的每个指针分配内存 */
12     for (t = 0; t<NUM; t++)
13     {
14         if ((str[t] = (char *)malloc(128)) == NULL)
15         {
16             printf("Allocation Error.\n");
17             exit(1);
18         }
19         /* 在分配的内存中存放字符串 */
20         printf("Enter string %d: ", t);
21         gets(str[t]);
22         //puts(str[t]);
23     }
24 
25     /* 释放内存 */
26     for (t = 0; t<NUM; t++)
27         free(str[t]);
28 
29     /* 由于主函数有返回值,故返回0值 */
30     return 0;
31 }

 

在堆内存中存放十个字符串并输出 堆栈溢出时退出程序

标签:数组   字符   enter   div   退出   def   turn   ring   col   

原文地址:https://www.cnblogs.com/liugangjiayou/p/11623518.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!