Solved

double size array initialize ?

Posted on 2006-10-24
8
1,641 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Check out the wrapper functions in the following links:
http://code.axter.com/allocate2darray.h
http://code.axter.com/allocate2darray.c
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:thefirstfbli
Comment Utility
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
Comment Utility
>>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
Comment Utility
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
Comment Utility
ok.. that's all thanks..
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now