Thanks to all of you because I solved the problem related with my
previous post.
I simply made confusion with pointers to pointers and then succeeded
passing the reference to the first element pointer.
:-))
You were right about by mixed code: quickly writing an example to
clarify the matter I made a C++ program, sorry for the inconvenience.
Anyway, although it all works fine with monodimensional arrays,
I still have a problem with irregular matrices (2D arrays with the
first dimension fixed, the second one variable).
I wrote a simple example code which perfectly works in the main
routine, while it arises an access violation if I put it in a
subroutine.
My purpose is to create an irregular matrix like this:
p[0][0] (no element)
p[1][0] p[1][1]
The two code blocks (in "main" and in "test") are identical, just
"p" changes in "*m".
The example is below.
Could you explain me why this occurs ?
Thanx again,
Morgan.
__________________________________________________ _______________
int main(int argc, char *argv[])
{
int **p;
p = (int **)malloc(2*sizeof(int *));
if (p == NULL) {printf("Error: Out of Memory \n"); exit(1);}
p[0] = (int *)malloc(sizeof(int));
if (p[0] == NULL) {printf("Error: Out of Memory \n"); exit(1);}
p[1] = (int *)malloc(2*sizeof(int));
if (p[1] == NULL) {printf("Error: Out of Memory \n"); exit(1);}
p[0][0] = 1; printf("%d\n",p[0][0]);
p[1][0] = 2; printf("%d\n",p[1][0]);
// Next line is normally executed here !!!!!!!!!!!!!!!!!!!
p[1][1] = 3; printf("%d\n",p[1][1]);
free(p);
test(&p);
printf("%d\n",p[0][0]);
printf("%d\n",p[1][0]);
printf("%d\n",p[1][1]);
free(p);
system("PAUSE");
return 0;
}
void test(int ***m)
{
*m = (int **)malloc(2*sizeof(int *));
if (*m == NULL) {printf("Error: Out of Memory \n"); exit(1);}
*m[0] = (int *)malloc(sizeof(int));
if (*m[0] == NULL) {printf("Error: Out of Memory \n"); exit(1);}
*m[1] = (int *)malloc(2*sizeof(int));
if (*m[1] == NULL) {printf("Error: Out of Memory \n"); exit(1);}
*m[0][0] = 1;
*m[1][0] = 2;
// Next line causes an access violation here !!!!!!!!!!!!!!!!!!!
*m[1][1] = 3;
// For the exactness it seems to me that the access violation
// arises after the last line, coming back to main... :-( !?
}
____________________________HELP !!!_____________________________