c - 具有动态矩阵的程序在到达 printf 时崩溃

标签 c arrays pointers

该程序的某些部分是西类牙语,因此我将进行解释:它是一个动态二维数组,会向用户打印一条消息,请求输入行数和列数。然后使用函数 aloc 为数组分配空间,其中 calloc 用于分配,第二个指针接收行数,然后使用 for() 循环为列分配空间。 问题出现在函数 imp() 中,它应该打印结果矩阵,我相信这是由于指针算法,但我不确定。程序崩溃了,但是编译过程没有问题,据我了解,所有操作都是有效的。

我尝试使用经典的 2D 数组形式,但它仍然与 ((a+i)+j) 的 a[i][j] 崩溃。我尝试在没有 printf 的情况下运行该程序,它会在到达 suma() 时崩溃,它应该将两个矩阵相加,我相信它在到达指针算法时也会崩溃。省略这些后,程序运行顺利。

#include<stdio.h>
#include<stdlib.h>
#define p printf
#define s scanf

float** aloc(float **m,int r,int c);
void asig(float **a,int r,int c);
void imp(float **a,int r,int c);
float** suma(float **a,float **b,float **c,int r,int x);

int main(int argc,char*argv[])
{
    float **ma=NULL,**mb=NULL,**mc=NULL;
    int r=0,c=0,i=0,j=0;
    p("\n\tEste programa suma 2 matrices cuyas dimensiones son dadas por el usuario\n\n\t%cCu%cntos renglones tienen las matrices?",168,160);
    s("%d",&r);
    p("\n\t%cCu%cntas columnas tienen las matrices?",168,160);
    s("%d",&c);
    ma=aloc(ma,r,c);
    mb=aloc(mb,r,c);
    mc=aloc(mc,r,c);
    p("\n\n\t\tMATRIZ 1");
    asig(ma,r,c);
    imp(ma,r,c);
    p("--------------------------\n\n\t\tMATRIZ 2");
    asig(mb,r,c);
    imp(mb,r,c);
    p("--------------------------");
    mc=suma(ma,mb,mc,r,c);
    p("\n\tLa matriz resultante es:\n");
    imp(mc,r,c);
    fflush(stdin);
    getchar();
    return 0;
}

float** aloc(float **m,int r,int c)
{
    int i=0;
    if((m=(float**)calloc(r,sizeof(float*)))==NULL)
    {
        p("Error al asignar espacio");
        exit(0);
    }
    for(i=0;i<r;i++)
        if((m[i]=(float*)calloc(c,sizeof(float)))==NULL)
        {
            p("Error al asignar espacio");
            exit(0);
        }
    return m;
}

void asig(float **a,int r,int c)
{
    int i=0,j=0;
    for(i=0;i<r;i++)
        for(j=0;j<c;j++)
        {
            p("\n\t%cCu%cl es el elemento [%d][%d] de la matriz?",168,160,i+1,j+1);
            s("%f",((a+i)+j));
        }
}

void imp(float **a,int r,int c)
{

    int i=0,j=0;
    p("\n\tLa matriz queda:\n");
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            p("%f ",*(*(a+i)+j));
        }
        p("\n");
    }
}

float** suma(float **a,float **b,float **c,int r,int x)
{
    int i=0,j=0;
    for(i=0;i<r;i++)
        for(j=0;j<x;j++)
            *(*(c+i)+j)=*(*(a+i)+j)+*(*(b+i)+j);
    return c;
}de here

最佳答案

问题来了

s("%f", ((a + i) + j));

这个

scanf("%f", &a[i][j]);

始终使用正确的函数名称,这个 (*(a + i) + j) 也可以,但正如您所看到的那样令人困惑。

使用数组索引符号来避免问题。

如果您想知道我是如何在您发布的困惑中找到它的,我使用了编译器警告。他们是你的 friend 。

BTW: scanf() 有一个返回值,检查一下,如果你不小心给程序一些无效的输入,那么就会发生意想不到的问题,同样对于每个 malloc() 你需要一个 free() 并且不要使用 calloc() 除非你打算稍后将值​​初始化为零,如果你将使用 malloc() 初始化每个值。

这里是你的程序的更正版本

#include <stdio.h>
#include <stdlib.h>

float** aloc(float **m,int r,int c);
void asig(float **a,int r,int c);
void imp(float **a,int r,int c);
float** suma(float **a,float **b,float **c,int r,int x);

int main()
{
    float **ma = NULL,**mb = NULL,**mc = NULL;
    int r = 0, c = 0;

    printf("\n\tEste programa suma 2 matrices cuyas dimensiones son dadas por el usuario\n\n\t%cCu%cntos renglones tienen las matrices?",168,160);
    if (scanf("%d", &r) != 1)
        return -1;
    printf("\n\t%cCu%cntas columnas tienen las matrices?", 168, 160);
    if (scanf("%d", &c) != 1)
        return -1;

    ma = aloc(ma, r, c);
    if (ma == NULL)
        goto failed;
    mb = aloc(mb, r, c);
    if (mb == NULL)
        goto failed;
    mc = aloc(mc, r, c);
    if (mc == NULL)
        goto failed;
    printf("\n\n\t\tMATRIZ 1");

    asig(ma, r, c);
    imp(ma, r, c);

    printf("--------------------------\n\n\t\tMATRIZ 2");

    asig(mb, r, c);
    imp(mb, r, c);

    printf("--------------------------");

    mc = suma(ma, mb, mc, r, c);

    printf("\n\tLa matriz resultante es:\n");

    imp(mc, r, c);
    getchar();

    free(ma);
    free(mb);
    free(mc);

    return 0;

failed:
    free(ma);
    free(mb);
    free(mc);

    return -1;
}

float** aloc(float **m,int r,int c)
{
    int i = 0;

    if ((m = calloc(r, sizeof(float*))) == NULL)
    {
        printf("Error al asignar espacio");
        return NULL;
    }

    for (i = 0 ; i < r ; i++)
    {
        if ((m[i] = calloc(c, sizeof(float))) == NULL)
        {
            int j;
            for (j = i ; j >= 0 ; j--)
                free(m[j]);
            free(m);
            return NULL;
        }
    }
    return m;
}

void asig(float **a,int r,int c)
{
    int i = 0, j = 0;
    for(i = 0 ; i < r ; i++)
    {
        for(j = 0 ; j < c ; j++)
        {
            printf("\n\t%cCu%cl es el elemento [%d][%d] de la matriz?", 168, 160, i + 1, j + 1);
            scanf("%f", &a[i][j]);
        }
    }
}

void imp(float **a,int r,int c)
{

    int i = 0,j = 0;
    printf("\n\tLa matriz queda:\n");
    for(i = 0 ; i < r ; i++)
    {
        for(j = 0 ; j < c ; j++)
        {
            printf("%10f", a[i][j]);
        }
        printf("\n");
    }
}

float** suma(float **a,float **b,float **c,int r,int x)
{
    int i = 0,j  = 0;
    for(i = 0 ; i < r ; i++)
    {
        for(j = 0 ; j < x ; j++)
        {
            c[i][j] = a[i][j] + b[i][j];
        }
    }
    return c;
}

关于c - 具有动态矩阵的程序在到达 printf 时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28609663/

相关文章:

c - 警告不兼容的指针,为什么会发生这种情况?

c - 对字符进行编码最有效的方法是什么? (关于内存)

arrays - 从字典创建有序数组

c++ - 复制构造函数中的 std::copy 出错

c++ - 无法通过 -> 运算符访问 vector 对象的功能

c++ - 在 Windows 上获取实际文件名(使用正确的大小写)

c - 使用 Arduino Uno r3 查找 int 值的倒数

c - 如何在递归函数中返回或复制字符串数组?

java - Java 中的接口(interface)数组

c - 输入一个函数 getWord (),它接受一个字符串并返回一个指向单词开头的指针