[英]pointer on 2d array in a void function
Well, I would like to initiate my 2D array by a void function. 好吧,我想通过void函数启动2D数组。 But I obtain a Segmentation Fault...
但是我遇到了细分错误...
That's my code : 那是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void groupeetchoixgen(int*** choixGEN);
void main(int args, char **argv)
{
int** choixGEN;
int i,j;
choixGEN=(int**) malloc (sizeof(int*)*2);
for (i=0; i<3; i++)
{
choixGEN[i]=(int*) malloc (sizeof(int)*3);
}
groupeetchoixgen(&choixGEN);
}
void groupeetchoixgen(int*** choixGEN)
{
(*(choixGEN)[1])[0]=1;
}
I Think that the trouble is (*(choixGEN)[1])[0]=1; 我认为麻烦是(*(choixGEN)[1])[0] = 1; But I don't know why !
但是我不知道为什么!
Thanks for your help 谢谢你的帮助
On this line: 在这行上:
choixGEN=(int**) malloc (sizeof(int*)*2);
you are only allocating space for 2 int*
s, but you access the 3rd element in the for
loop. 您只为2
int*
分配空间,但是您访问了for
循环中的第3个元素。
您仅为两个(int*)
分配内存,但是尝试引用choixGEN[0],choixGEN[1],choixGEN[2]
,即3
In General, SegFault
in C
means: SegFault
, C
SegFault
表示:
So, most likely, your problem is in this line: 因此,很可能您的问题出在以下这一行:
choixGEN=(int**) malloc (sizeof(int*)*2);
You declared a int*** choixGEN
but only allocated memory for (int**)
您声明了一个
int*** choixGEN
但只为(int**)
分配了内存
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.