thefirstfbli
asked on
double size array initialize ?
i dont know size of the array orginally. it is double size array.
how to initialize it at the begining of the program ?
how to initialize it at the begining of the program ?
ASKER
expected size is not too big.
actually i want to learn its sense, logic.
i do it like above,
double *p;
p=(*double) malloc(sizeof(double) * what ? )
what is the actual size ?
and also, how can i reach the integer value @ point[1][2], if it is 8*8 array. like *(p+i) ? i is counter.
actually i want to learn its sense, logic.
i do it like above,
double *p;
p=(*double) malloc(sizeof(double) * what ? )
what is the actual size ?
and also, how can i reach the integer value @ point[1][2], if it is 8*8 array. like *(p+i) ? i is counter.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Check out the wrapper functions in the following links:
http://code.axter.com/allocate2darray.h
http://code.axter.com/allocate2darray.c
http://code.axter.com/allocate2darray.h
http://code.axter.com/allocate2darray.c
ASKER
thanks deepudeepam , it is really good .
last thing about the line; " double *p[] " to understand,
double's is in the code because numbers can be large, right ? instead of double may i use int ?
and why we use *p[], not only *p ?
thanks so much.
last thing about the line; " double *p[] " to understand,
double's is in the code because numbers can be large, right ? instead of double may i use int ?
and why we use *p[], not only *p ?
thanks so much.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here's example for creating a buffer for a 2-D array:
void **Allocate2DArray(int TypeSize, int x, int y)
{
void **ppi = malloc(x*sizeof(void*));
void *pool = malloc(x*y*TypeSize);
unsigned char *curPtr = pool;
int i;
if (!ppi || !pool)
{ /* Quit if either allocation failed */
if (ppi) free(ppi);
if (pool) free(pool);
return NULL;
}
for(i = 0; i < x; i++)
{
*(ppi + i) = curPtr;
curPtr += y*TypeSize;
}
return ppi;
}
Example usage:
int **My2DChar = Allocate2DArray(sizeof(int ), x, y);
void **Allocate2DArray(int TypeSize, int x, int y)
{
void **ppi = malloc(x*sizeof(void*));
void *pool = malloc(x*y*TypeSize);
unsigned char *curPtr = pool;
int i;
if (!ppi || !pool)
{ /* Quit if either allocation failed */
if (ppi) free(ppi);
if (pool) free(pool);
return NULL;
}
for(i = 0; i < x; i++)
{
*(ppi + i) = curPtr;
curPtr += y*TypeSize;
}
return ppi;
}
Example usage:
int **My2DChar = Allocate2DArray(sizeof(int
ASKER
ok.. that's all thanks..
If it's less than a million or so, it's often simpler to just declare it to be that size. That's assuming this is running on some reasonably modern computer with many megabytes of memory.
For larger sizes, use malloc:
double * Array;
Array = malloc( sizeof( double ) * TheActualSize );
\
.... use Array here as usual.
free( Array ); // when you're done using the array.