0

Well, I would like to initiate my 2D array by a void function. 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; But I don't know why !

Thanks for your help

2
  • can you paste your error message here? Commented Jul 20, 2012 at 19:43
  • shouldn't it just be (*choixGEN)[1][0] = 1; ? Commented Jul 20, 2012 at 19:44

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

You only allocate memory for two (int*) but you try to reference choixGEN[0],choixGEN[1],choixGEN[2] which is 3

Comments

0

In General, SegFault in C means:

  1. You're accessing a location in memory which was not allocated.
  2. You're using a dangling pointer to access a memory location

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**)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.