Solved

double size array initialize ?

Posted on 2006-10-24
8
1,650 Views
Last Modified: 2012-08-14
i dont know size of the array orginally. it is double size array.

how to initialize it at the begining of the program ?
0
Comment
Question by:thefirstfbli
8 Comments
 
LVL 22

Expert Comment

by:grg99
ID: 17794833
What's the maximum expected size?    

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.



0
 
LVL 1

Author Comment

by:thefirstfbli
ID: 17794975
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.
0
 
LVL 8

Accepted Solution

by:
deepu chandran earned 200 total points
ID: 17795126
hi,

>>and also, how can i reach the integer value @ point[1][2], if it is 8*8 array. like *(p+i) ? i is counter.

That means you are using Two dimentional array,
double *p[];
p=(double *)malloc(size(double)*(8*8));
Through this you can allocate a two dimensional array.
you can assign the values as,
*(*(p+i)+j)=0.0  >>where i and j are two integers

i think this will help you

Deepu
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 30

Expert Comment

by:Axter
ID: 17795261
Check out the wrapper functions in the following links:
http://code.axter.com/allocate2darray.h
http://code.axter.com/allocate2darray.c
0
 
LVL 1

Author Comment

by:thefirstfbli
ID: 17795405
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.

0
 
LVL 30

Assisted Solution

by:Axter
Axter earned 100 total points
ID: 17795455
>>instead of double may i use int ?

Yes

>>*p[], not only *p

In order to traverse a variable using two sets of brackets ([x][y]), you need a pointer of pointers.
You can do this by either of the following methods:
double *p[];
or
double **p;

I recommend using **, but that's a personal opinion.

If you use only a single dimesional variable type (*p), then you would need to add extra logic to your single bracket ([x]) to traverse a 2-dimensional buffer.

p[x + y * LenX] = 123;
0
 
LVL 30

Expert Comment

by:Axter
ID: 17795473
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);
0
 
LVL 1

Author Comment

by:thefirstfbli
ID: 17795479
ok.. that's all thanks..
0

Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question