Solved

double size array initialize ?

Posted on 2006-10-24
8
1,669 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
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 for-loops in the C programming language.

623 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