Solved

double size array initialize ?

Posted on 2006-10-24
8
1,643 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
 
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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

895 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

11 Experts available now in Live!

Get 1:1 Help Now