how to make dynamic array with known number of column element
for example for a 3d coordinate.
suppose i need to make 2d dynamic array for a 3d coordinate, because the element of 3d coordinate is x, y, z so the number column is 3.
let's say i need to have
double point[some_number][3];
and that number is suppose unknown until we read the file, lets say ptscount.
one simple way to allocate the dynamic 2d array is as follow.
double (*point)[3] = (double (*)[3])malloc(sizeof(double) * ptscount*3);
brief explanation
- double (*point)[3]: double variable of "point" that has 3 array element, this will make point[ptscount][3]
- (double (*)[3]): casting to double of pointer that has 3 array element
- malloc(sizeof(double) * ptscount*3): memory allocation for (the size of double variable) times(×) (ptscount (number or row of the array) with 3 column)
after using it you have to free the memory
free (point);
2d dynamic array with known number of column element
Friday, June 9, 2017
You Might Also Like
centripetal parametrization for curve fitting or interpolation
1. playing with string manipulation, 2. function to sort dynamically alocated string array in visual c++
Simple basic archimedean spiral code implementation
Simple basic spiral code implementation 2 with tolerance 0.1
simple example of using operator overloading in c++
compute derivative coefficient of n-th degree polynomial equation
Browser Comparison: Firefox quantum vs Chrome
Subscribe to:
Post Comments (Atom)
No comments