| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 500 人关注过本帖
标题:关于malloc函数的,请各位看看错在哪?
只看楼主 加入收藏
独孤鹏
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-4-3
结帖率:100%
收藏
已结贴  问题点数:10 回复次数:4 
关于malloc函数的,请各位看看错在哪?
/*malloc函数的使用*/
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
main()
{
    int *p,*table;
    int size;
    printf("\nwhat is the size of table?");
    scanf("%d",size);
    printf("\n");
    if((table=(int*)malloc(size*sizeof(int)))==NULL)
    {
        printf("no space available");
        exit(1);
    }
    else
    {
    printf("\n address of the first byte is %u\n",table);
    printf("\n input table value\n");
    for(p=table;p<table+size;p++)
        scanf("%d",p);
    for(p=table+size-1;p>=table;p--)
        printf("%d is stored in address %u \n",*p,p);
    }
    }
搜索更多相关主题的帖子: malloc 函数 
2010-05-02 12:35
dousao
Rank: 2
等 级:论坛游民
帖 子:228
专家分:58
注 册:2007-11-8
收藏
得分:0 
报什么错?malloc就要free,对称的。

同行售各专业书籍,全1折,淘宝店:http://maishu.
2010-05-02 14:10
独孤鹏
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-4-3
收藏
得分:0 
回复 2楼 dousao
just have a try
2010-05-02 14:26
jcslt
Rank: 8Rank: 8
来 自:90-xx.com
等 级:蝙蝠侠
帖 子:251
专家分:975
注 册:2009-10-10
收藏
得分:10 
错误挺多的,猜了下你的功能改成如下:
#include<stdio.h>
#include<stdlib.h>
//#define NULL 0//重复定义
int main()
{
    int *table;
    int size,i;
    printf("\nwhat is the size of table?");
    scanf("%d",&size);//
    printf("\n");
    if((table=(int*)malloc(size*sizeof(int)))==NULL)
    {
        printf("no space available");
        exit(1);
    }
    else
    {
    printf("\n address of the first byte is %p\n",table);//或%x,不过这个地址是随机的,每次都会不一样
    printf("\n input table value\n");
    for(i=0;i<size;i++)
        scanf("%d",table+i);
    for(i=size-1;i>=0;i--)
        printf("%d is stored in address %p \n",*(table+i),table+i);
    }
    free(table);
    return 0;
}

www.
2010-05-02 14:35
独孤鹏
Rank: 1
等 级:新手上路
帖 子:36
专家分:0
注 册:2010-4-3
收藏
得分:0 
回复 4楼 jcslt
多谢
2010-05-02 15:36
快速回复:关于malloc函数的,请各位看看错在哪?
数据加载中...
 
   



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

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