在matrice中的c指针练习[关闭]

问题描述 投票:-2回答:3

我试图让i程序在指针上用matrice创建默认数字但是没有得到结果,没有任何错误。有人能帮助我吗?我使用“Dev C”作为IDE。 .................................................. .................................................. .......................................

    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <Math.h>
    typedef struct S_Matrice
    {
            int L;
            int C;
            int * mat ;
            }Matrice;
            //creation du matrice
    Matrice *CreerMatrice(int l, int c)
    {
    Matrice *m ;
    m = (Matrice *)malloc(sizeof(Matrice)) ;
    m->L = l ;
    m->C = c ;
    m->mat = (int *)malloc(l * c* sizeof(int)) ;
      return m;
    }
    //remplir une matrice
    Matrice *remplirMat(int l, int c)
    {
    Matrice *m ;
    m->mat = (int *)malloc(l * c* sizeof(int)) ; 
    m->L = l ;
    m->C = c ;
        int i,j;
        for(i=0; i < l;i++)
        {
            for(j=0; j <c; j++)
            m->mat=(int *)(rand()%26 + 'a');
        }
        return m;
     }
     void afficher(Matrice *m, int l, int c) {
      int i, j;
    m->L = l ;
    m->C = c ;
      for (i = 0; i < m->L; i++) {
        for (j = 0; j < m->L; j++)
          printf("%d ",m);
        printf("\n");
      }
    }

    int main() {
      int c = 8;
      int l = 8;
       Matrice *mat= CreerMatrice(c,l);
      remplirMat(8,8);
      afficher(mat,8,8);
      printf("bravo");
      getch();
      return 0;
    }
c
3个回答
1
投票

在remplirMat你有

Matrice *m ;
m->mat = (int *)malloc(l * c* sizeof(int)) ; 

你做m->mat而m没有初始化

缺少m = (Matrice *)malloc(sizeof(Matrice));

之后 :

pi@raspberrypi:/tmp $ ./a.out
11972616 11972616 11972616 11972616 11972616 11972616 11972616 11972616 
11972616 11972616 11972616 11972616 11972616 11972616 11972616 11972616 
11972616 11972616 11972616 11972616 11972616 11972616 11972616 11972616 
11972616 11972616 11972616 11972616 11972616 11972616 11972616 11972616 
11972616 11972616 11972616 11972616 11972616 11972616 11972616 11972616 
11972616 11972616 11972616 11972616 11972616 11972616 11972616 11972616 
11972616 11972616 11972616 11972616 11972616 11972616 11972616 11972616 
11972616 11972616 11972616 11972616 11972616 11972616 11972616 11972616 
bravo

所有时间都是相同的值,因为remplirMat中的行

m->mat=(int *)(rand()%26 + 'a');

你忘记了索引i和j存储到m-> mat中保存的已分配数组中,可能必须是:

m->mat[i*c +j]=(rand()%26 + 'a');

在你没有很好地写好矩阵之后

printf("%d ",m);

尝试编写分配的数组地址,需要编写元素:

printf("%d ",m->mat[i*c +j]);

但是只写0 ...因为在主要:

  remplirMat(8,8);

必须是填充矩阵丢失

  mat=remplirMat(8,8);

事实上Matrice *mat= CreerMatrice(c,l);是无用的,因为分配的矩阵丢失了。可能remplirMat必须在参数中获取矩阵而不是分配新的矩阵

所以更好地替换:

Matrice *remplirMat(int l, int c)
{
  Matrice *m ;
  m = (Matrice *)malloc(sizeof(Matrice)) ;
  ...
  return m;
}

通过

void remplirMat(Matrice *m, int l, int c)
{
   ...
}

当然还有主要的替代品

 Matrice *mat= CreerMatrice(c,l);
 mat=remplirMat(8,8);

by Matrix * mat = CreateMatrix(c,l);

 remplirMat(mat, 8,8);

编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wextra ma.c
pi@raspberrypi:/tmp $ ./a.out
110 119 108 114 98 98 109 113 
98 104 99 100 97 114 122 111 
119 107 107 121 104 105 100 100 
113 115 99 100 120 114 106 109 
111 119 102 114 120 115 106 121 
98 108 100 98 101 102 115 97 
114 99 98 121 110 101 99 100 
121 103 103 120 120 112 107 108 
bravo

我鼓励您添加FreeMatrice以释放分配的资源

void FreeMatrice(Matrice *m)
{
  free(m->mat);
  free(m);
}

并在之后称之为main:

pi@raspberrypi:/tmp $ gcc -g -pedantic -Wextra ma.c
pi@raspberrypi:/tmp $ valgrind --leak-check=full ./a.out
==12798== Memcheck, a memory error detector
==12798== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==12798== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==12798== Command: ./a.out
==12798== 
110 119 108 114 98 98 109 113 
98 104 99 100 97 114 122 111 
119 107 107 121 104 105 100 100 
113 115 99 100 120 114 106 109 
111 119 102 114 120 115 106 121 
98 108 100 98 101 102 115 97 
114 99 98 121 110 101 99 100 
121 103 103 120 120 112 107 108 
bravo
==12798== 
==12798== HEAP SUMMARY:
==12798==     in use at exit: 0 bytes in 0 blocks
==12798==   total heap usage: 4 allocs, 4 frees, 2,316 bytes allocated
==12798== 
==12798== All heap blocks were freed -- no leaks are possible
==12798== 
==12798== For counts of detected and suppressed errors, rerun with: -v
==12798== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)

0
投票

欢迎来到SO!请注意,函数remplirMat返回的矩阵结构正在丢失,因为您没有捕获返回值。

因为你有一个整数数组中的实际数据,你需要插入到该数组中的行m->mat=(int *)(rand()%26 + 'a');不断重新定义m->mat是什么。看起来你正在尝试制作一个二维数组来保存矩阵数据,所以要插入矩阵,我会做类似的事情:

int i,j;
for(i = 0; i < m->L, i++)     // assuming m->L is the number of rows
    for(j=0; j< m->C, j++)        // assuming m-C is the number of columns
        m[i*(m->L) + j] = (rand()%26 + 'a');

好的,那么这里发生了什么?

  1. m->mat = (int *)malloc(l * c* sizeof(int)) ;分配足够的内存来保存l * c整数,实际上m->mat是一个整数数组。现在,如何将矩阵的数据存储到阵列中完全取决于您,您只需要保持一致。从代码的外观来看,您将以行主格式存储数据,即将第一行的元素放入数组,然后放入第二行。
  2. 术语i*(m->L) + j是一种将(行,列)形式的索引转换为单个索引的惯用方法。例如,如果我有一个4x4数组,我需要找出在你所在的一维数组中可以找到A(3,4)(第三行和第四列中的元素)的元素的位置创建。我们使用公式row*MAX_ROWS + col

考虑以下转换(假设您正在使用嵌套循环),其中变量行是您的变量i,变量col是变量'j'。 MAX_ROWS设置为4

element         linear index    row      col        row*MAX_ROWS + col   
A(1,1)             0             0        0           0*4 + 0 = 0
A(1,2)             1             0        1           0*4 + 1 = 2
A(1,3)             2             0        2           0*4 + 2 = 2
A(1,4)             3             0        3           0*4 + 3 = 4
A(2,1)             4             1        0           1*4 + 0 = 4

因此,您可以看到从行,列到单个索引的映射是如何工作的。

鉴于所有这些,我会修改你的程序如下(我也摆脱了一个字符变量名称),我已经将CreerMatrice和remplierMat的功能组合到一个函数中。我还必须删除getch(),因为它是一个仅限Windows的API,我在Linux机器上进行了测试。

typedef struct S_Matrice
{
        int rows;
        int cols;
        int * mat ;
} Matrice;

//creation du matrice
Matrice *CreerMatrice(int rows, int cols )
{
    Matrice *m ;
    int ndx,jdx;

    m = (Matrice *)malloc(sizeof(Matrice)) ;
    m->rows = rows ;
    m->cols = cols ;
    m->mat = (int *)malloc(rows * cols * sizeof(int)) ;


    for(ndx=0; ndx < rows; ndx++)
    {
        for(jdx=0; jdx < cols; jdx++)
        m->mat[ndx*rows + jdx] = (rand()%26 + 'a');
    }

    return m;
 }

void afficher(Matrice *m) 
{
  int ndx, jdx;
  for (ndx = 0; ndx < m->rows; ndx++) 
  {
      for (jdx = 0; jdx < m->cols; jdx++)
          printf("%d ", m->mat[ndx*n->rows + jdx]);
      printf("\n");
  }
}

int main() 
{
    int c = 8;
    int l = 8;
    Matrice *mat= CreerMatrice(c,l);
    afficher(mat);
    printf("bravo");
    //getch();
    return 0;
}

上面的代码使用gcc 5.4.0和命令行gcc -std=c99 -pedantic -Wall在ubuntu上编译并生成输出:

110 119 108 114 98 98 109 113 
98 104 99 100 97 114 122 111 
119 107 107 121 104 105 100 100 
113 115 99 100 120 114 106 109 
111 119 102 114 120 115 106 121 
98 108 100 98 101 102 115 97 
114 99 98 121 110 101 99 100 
121 103 103 120 120 112 107 108 
bravo

-1
投票

问题出在remplirMat。它应该是:

Matrice *remplirMat(int l, int c)
{
    Matrice *m= malloc(sizeof(Matrice));
    m->mat = (int *)malloc(l * c* sizeof(int)); 
    m->L = l;
    m->C = c;
    for(int i=0; i < l;i++)
    {
        for(int j=0; j <c; j++)
            m->mat + (i*m->C) + j= (int)(rand()%26 + 'a');
    }
    return m;
 }

首先,必须将记忆分配给m。接下来,您要将m->mat[i][j]设置为随机字母。但是因为编译器不知道行和列的大小,你必须自己进行地址计算:地址是mat加上行数(i*m->C)加上当前列j

© www.soinside.com 2019 - 2024. All rights reserved.