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
Solved

double size array initialize ?

Posted on 2006-10-24
8
1,655 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

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…
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 and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

839 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