| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1315 人关注过本帖
标题:realloc()
只看楼主 加入收藏
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
结帖率:100%
收藏
 问题点数:0 回复次数:2 
realloc()
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int n;
    int i;
    int * array;

    array = (int*)malloc(sizeof(int));
    printf("把元素赋值:");
    for(n = 0;
       NULL != (array = (int*)realloc(array, sizeof(int) * (n + 1)))
       && scanf("%d", array + n) == 1;
       n++)
        continue;  // 可换为空语句或,NULL;

    for(i = 0; i < n; i++)
        printf("%d ", array[i]);

    free(array);
    system("pause");
    return 0;
}
搜索更多相关主题的帖子: array int realloc sizeof include 
2008-02-18 22:58
makewelldone
Rank: 1
来 自:江苏南京
等 级:新手上路
帖 子:97
专家分:0
注 册:2006-9-25
收藏
得分:0 
回复
你用MALLOC和REALLOC还不如用MEMSET来分配空间,MALLOC和REALLOC回产生堆
2008-02-24 11:35
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
memset()

功 能: 设置s中的所有字节为ch, s数组的大小由n给定
用 法: void *memset(void *s, char ch, unsigned n);

—>〉Sun〈<—
2008-02-24 13:00
快速回复:realloc()
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.029863 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved