标题:分配动态二维数组问题,求改错!
取消只看楼主
justlxy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:28
专家分:158
注 册:2013-5-14
结帖率:100%
已结贴  问题点数:20 回复次数:1 
分配动态二维数组问题,求改错!
# include <stdio.h>
# include <stdlib.h>
int main()
{
    void fun(int **p, int x, int y)
    {
        int i, j;
        p = (int **)malloc(x * sizeof(int *));
        for (i = 0; i < x; i++)
        {
            p[i] = (int *)malloc(y * sizeof(int));
            for (j = 0; j < y; j++)
            {
                p[i][j] = i+j;
                printf("%5d",p[i][j]);
            }
        }
    }
    void funs(int **p, int x)
    {
        int i, j;
        for (i = 0; i < x; i++)
        {
            
            free(p[i]);
            pArr[i] = 0;   
        }
        free(p);
        p = 0;
    }
    int **p=NULL;
    fun(p,4,5);
    funs(p,4);
    return 0;
}
搜索更多相关主题的帖子: include 动态 void fun 
2013-06-04 14:57
justlxy
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:28
专家分:158
注 册:2013-5-14
得分:0 
回复 3楼 小xiong
我明白了,但是修改后还是有问题啊,(funs函数有问题),求纠错!
代码如下:
程序代码:
# include <stdio.h>
# include <stdlib.h>
void fun(int **p, int x, int y)
{
    int i, j;
    p = (int **)malloc(x * sizeof(int *));
    for (i = 0; i < x; i++)
    {
        p[i] = (int *)malloc(y * sizeof(int));
        for (j = 0; j < y; j++)
        {
            p[i][j] = i+j;
            printf("%5d",p[i][j]);
        }
    }
}
void funs(int **p, int x)
{
    int i;
    for (i = 0; i < x; i++)
    {
        free(p[i]);
        p[i] = NULL;   
    }
    free(p);
    p =NULL;
}
int main()
{
    int **p=NULL;
    fun(p,4,5);
    funs(p,4);
    return 0;
} 
2013-06-05 22:40



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-414491-1-1.html




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

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