[Dev-C++] function for allocate memory for 2D vectors
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <and...@su...> - 2007-11-11 05:28:12
|
Dear Users,
Could you confirm if this function is
correct?
The reason of my question is: inspecting
the address of *vect in line x in the function
is different of line y. I was expecting equal
addresses, wasn´t?
////////// code /////////////////////////
int Alocate2DVect(int ***vect, int lin, int col)
{
int i, *pont;
*vect = (int **) malloc(lin*sizeof(int)); // line x
if(!vect) return(0);
for(i=0; i<col; i++) *(*vect + i) = (int *) malloc(col*sizeof(int));
if(!vect) return(0); // line y
return(1);
}
int main()
{
int **vector2D;
int i, j, lin=3, col=4;
if(!Alocate2DVect(&vector2D, lin, col)) exit(1);
for(i=0; i<lin; i++)
for(j=0; j<col; j++)
vector2D[i][j] = i + j;
for(i=0; i<lin; i++){
for(j=0; j<col; j++){
printf("%d\t", vector2D[i][j]);
}
printf("\n");
}
free(vector2D);
system("PAUSE");
return 0;
}
//////////// end of code /////////
Regards
Andre
No virus found in this outgoing message.
Checked by AVG Free Edition.
Version: 7.5.503 / Virus Database: 269.15.28/1123 - Release Date: 10/11/2007
15:47
|