简体   繁体   English

2D数组上的指针

[英]pointer on 2D array

I have a segmentation fault and would like to know where is my mistake. 我的细分错误,想知道我的错误在哪里。

Let me explain. 让我解释。

In my main, I declare a 3D array: int*** Matricegroupegeneralisant 我主要声明了一个3D数组: int*** Matricegroupegeneralisant

Then this main uses a function recuperationinfoFich(&matricegroupegeneralisant); 然后,该主体使用函数recuperationinfoFich(&matricegroupegeneralisant); This function is declared as : recuperationinfoFich(int* * * * matricegroupegeneralisant) 该函数声明为: recuperationinfoFich(int* * * * matricegroupegeneralisant)

This function recuperationinfoFich uses another function recuperationmatricegroupesgeneralisants(matricegroupegeneralisantA[Ni]); 此函数recuperationinfoFich使用另一个功能recuperationmatricegroupesgeneralisants(matricegroupegeneralisantA[Ni]); This function is declared as recuperationmatricegroupesgeneralisants( int*** matricegroupegeneralisant) 此函数声明为recuperationmatricegroupesgeneralisants( int*** matricegroupegeneralisant)

My code: 我的代码:

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

void allocationdynamiquetableautroisdimdentier(int**** Matrice,int nbniveau, int nbligne, int nbcolonne)
{
int i,j;
    *Matrice=(int***) malloc (sizeof(int**)*nbniveau);
    for (i=0; i<nbniveau; i++)
    {
        (*(Matrice))[i]=(int**) malloc (sizeof(int*)*nbligne);  // allocation dynamique de la matrice Matrice
        for (j=0; j<nbligne; j++)
        {
            ((*(Matrice))[i])[j]=(int*) malloc (sizeof(int)*nbcolonne);
        } 
    }
}


void recuperationmatricegroupesgeneralisants(int*** matricegroupegeneralisantA)
{
    (*matricegroupegeneralisantA)[0][1]=1;
}

void recuperationinfoFich(int**** matricegroupegeneralisantA)
{
    allocationdynamiquetableautroisdimdentier(matricegroupegeneralisantA,3, 3, 7);
    recuperationmatricegroupesgeneralisants(matricegroupegeneralisantA[1]);
}



void main(int args, char **argv)
{

    int*** matricegroupegeneralisantA;

    recuperationinfoFich(&matricegroupegeneralisantA);
}

With Gdb : 使用Gdb:

(gdb) r
Starting program: /home/larimsna1/Desktop/a.out 

Breakpoint 1, 0x000000000040061a in main ()
(gdb) n
Single stepping until exit from function main,
which has no line number information.

Program received signal SIGSEGV, Segmentation fault.
0x00000000004005c8 in recuperationmatricegroupesgeneralisants ()
(gdb) 

I suspect the problem has something to do with your allocation function. 我怀疑问题与您的分配功能有关。 That being said, there are numerous functional and stylistic issues with the code you provided. 就是说,您提供的代码存在许多功能和风格方面的问题。 In one place, you failed to dereference a pointer enough times, causing you to assign an integer to a pointer type. 在一处,您未能多次取消对指针的引用,从而导致为指针类型分配了整数。 You needlessly use pointers for most of your transactions when you could simply not pass an argument and use a return value. 当您无法简单地传递参数并使用返回值时,您就不必在大多数事务中使用指针。

This code should do what you want, and should work properly, and is easier to read: 这段代码应该可以完成您想要的事情,并且应该可以正常工作,并且更易于阅读:

// allocation, returns pointer to allocated value
int *** allocationdynamique(int nbniveau, int nbligne, int nbcolonne)
{
    int *** Matrice;
    int i, j;
    Matrice = (int ***) malloc (sizeof(*Matrice) * nbniveau);
    for (i = 0; i < nbniveau; ++i)
    {
        Matrice[i] = (int **) malloc (sizeof(**Matrice) * nbligne);
        for (j = 0; j < nbligne; ++j)
        {
            Matrice[i][j] = (int *) malloc (sizeof(***Matrice) * nbcolonne);
        } 
    }
    return Matrice;
}

// computation, takes 2D array, modifies in place
void recuperationmatrice(int** matrice)
{
    matrice[0][1] = 1;
}

// you shouldn't use void main, its not part of the standard
int main(int args, char **argv)
{
    int*** matrice = allocationdynamique(3, 3, 7);
    recuperationmatrice(matrice[1]);

    return 0; 
}

Also, from a stylistic perspective, your variable names are ridiculously long, and you should use spaces around operators. 另外,从样式角度来看,变量名太长了,您应该在运算符周围使用空格。

A lot of the mistakes in this code are mistakes the compiler is capable of detecting and warning you about. 此代码中的许多错误是编译器能够检测到并警告您的错误。 They are valid C code that will compile properly, but in this and most other cases are written in error and will not work as intended. 它们是可以正确编译的有效C代码,但是在这种情况下以及大多数其他情况下,它们都是错误编写的,因此无法按预期工作。 You should compile with compiler warnings to weed out possible accidents: 您应该使用编译器警告进行编译,以清除可能的事故:

gcc -Wall -c file.c -o file.o

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM