Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic array sizing

Status
Not open for further replies.

jaybe38

Programmer
Jan 22, 2002
16
NZ
Hi,

I'm looking for a way to resize an array dynamically, or (even better) combine multiple arrays like so:

{array-1}
|
{array-2}
{array-2}
|
{array-3}

array-1 and array-3 are single-item, but all are arrays of a structure. Resizing would do, because I could define a small array and simply add cells each time I need to.

Any ideas?
 
Oh, and having read other answers to other posts, I'm sorry to say that the linked list is a no-go. The array is a parameter to an external function, which I can't alter.

 
jaybe,

Resizing array at runtime is not in the specification of the language .you can use lists as you have already
stated.

but yes you can combine multiple arrays, here is just a way

#define MAXSIZE 10

int arr1[MAXSIZE];
int arr2[MAXSIZE];
int arr3[MAXSIZE];
//define a array of int pointer

int *arrptr[MAXSIZE]={arr1,arr2,arr3};

you can add any number of array's to this arrptr and
manipulate things as you wish.


hope this helps.

yogesh





 
Hi Yogesh,

Sounds good, but can I then use the array as if it were one large one? Surely I would have to access the array at the pointer given, then find the index of the cell I need?

The function I have to feed it into needs an end result of one long array.

If I'm misunderstanding your answer, please let me know.

Jamie
 
are you looking for something like realloc? You could have an array and if the size is exceeded, you could re-alloc it and keep the original data intact. here is a cut and paste from MSDN

void *realloc( void *memblock, size_t size );

***************************************
realloc returns a void pointer to the reallocated (and possibly moved) memory block. The return value is NULL if the size is zero and the buffer argument is not NULL, or if there is not enough available memory to expand the block to the given size. In the first case, the original block is freed. In the second, the original block is unchanged. The return value points to a storage space that is guaranteed to be suitably aligned for storage of any type of object. To get a pointer to a type other than void, use a type cast on the return value.

****************************************

The realloc function changes the size of an allocated memory block. The memblock argument points to the beginning of the memory block. If memblock is NULL, realloc behaves the same way as malloc and allocates a new block of size bytes. If memblock is not NULL, it should be a pointer returned by a previous call to calloc, malloc, or realloc.

The size argument gives the new size of the block, in bytes. The contents of the block are unchanged up to the shorter of the new and old sizes, although the new block can be in a different location. Because the new block can be in a new memory location, the pointer returned by realloc is not guaranteed to be the pointer passed through the memblock argument.



Hope that helped... if not pls post again

Matt



 
jamie,

you may surely use array as if it were a very large one

i will write a small routine which might be of help

here i am assuming that all the elements in the array is filled, you can modify the code to suite your requirement.


#define MAX 5
void func( int **a)
{

int *x;
while(*a)
{
x=*a;
while(x < *a+MAX)
printf(&quot;%d\n&quot;,*x++);

a++;
}
}

void main (void)
{
int a[MAX]={0};
int b[MAX]={0};
int c[MAX]={0};

int *arrPtr[MAX]={a,b,c};
int k=1;
for(int x=0;x<MAX;x++)
{
a[x]=k+x;
b[x]=k*x;
c[x]=a[x]+b[x];
}

func( arrPtr);
}
hope this helps

bye
yogesh

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top