[英]pointer on 2d array in a void function
好吧,我想通過void函數啟動2D數組。 但是我遇到了細分錯誤...
那是我的代碼:
#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;
}
我認為麻煩是(*(choixGEN)[1])[0] = 1; 但是我不知道為什么!
謝謝你的幫助
在這行上:
choixGEN=(int**) malloc (sizeof(int*)*2);
您只為2 int*
分配空間,但是您訪問了for
循環中的第3個元素。
您僅為兩個(int*)
分配內存,但是嘗試引用choixGEN[0],choixGEN[1],choixGEN[2]
,即3
SegFault
, C
SegFault
表示:
因此,很可能您的問題出在以下這一行:
choixGEN=(int**) malloc (sizeof(int*)*2);
您聲明了一個int*** choixGEN
但只為(int**)
分配了內存
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.