473,322 Members | 1,540 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

"expandable" arrays - using pointers ... How to ?

Hi,

I want to create a container structure (array/list etc), that is expandable.

Currently, I have static arrays like this

SomeStruct *ptrArray[MAX_NUM_STRUCT] ;

What I want to do is to be able to use ptrs to ptrs like so:

SomeStruct **ptrArray ;

However, I am unsure as to how to do the ff after the declaration above:

1). Allocate memory (both initially and for subsequent data structure
increases)
2). Navigate the array - i.e. index to specific element in the array

mtia

Nov 15 '05 #1
2 3201

Alfonso Morra wrote:
Hi,

I want to create a container structure (array/list etc), that is expandable.

Currently, I have static arrays like this

SomeStruct *ptrArray[MAX_NUM_STRUCT] ;

What I want to do is to be able to use ptrs to ptrs like so:

SomeStruct **ptrArray ;

However, I am unsure as to how to do the ff after the declaration above:

1). Allocate memory (both initially and for subsequent data structure
increases)
2). Navigate the array - i.e. index to specific element in the array

mtia


Use realloc() to create and extend a dynamic buffer. You can access
individual elements as you would with a normal array. Here's an
untested example:

#include <stdlib.h>

typedef struct {...} SomeStruct;

SomeStruct **extendArray(SomeStruct **arr, size_t *cursize, size_t
extent)
{
SomeStruct **tmp;

/**
* realloc() will return NULL on failure, address of the
* first element in the buffer on success. In order to
* extend the buffer, realloc() may move it to a
* different location, so the base address may change.
*/
tmp = realloc(arr, sizeof arr[0] * (*cursize + extent));
if (tmp)
{
arr = tmp;
*cursize += extent;
}
return arr;
}

int main(void)
{
/**
* Make sure array pointer is initially NULL
*/
SomeStruct **myArr = NULL;
size_ t arrSize = 0;
size_t i;

/**
* Initially allocate 20 elements to myArr
*/
myArr = extendArray(myArr, &arrSize, 20);
if (arrSize != 20)
{
/**
* Allocation failed; fatal error
*/
return EXIT_FAILURE;
}

for (i = 0; i < arrSize; i++)
{
/**
* do something with myArr[i]
*/
}

/**
* Extend array by another 5 elements
*/
myArr = extendArray(myArr, &arrSize, 5);
if (arrSize != 25)
{
/**
* Attempt to extend existing array failed.
*/
return EXIT_FAILURE;
}

for (i = 20; i < arrSize; i++)
{
/**
* do something with myArr[i]
*/
}

return EXIT_SUCCESS;
}

There are probably a hundred better ways to indicate success or
failure, and you'll want to add some intelligent error handling, but
that should give you the basic idea.

Nov 15 '05 #2


John Bode wrote:
Alfonso Morra wrote:
Hi,

I want to create a container structure (array/list etc), that is expandable.

Currently, I have static arrays like this

SomeStruct *ptrArray[MAX_NUM_STRUCT] ;

What I want to do is to be able to use ptrs to ptrs like so:

SomeStruct **ptrArray ;

However, I am unsure as to how to do the ff after the declaration above:

1). Allocate memory (both initially and for subsequent data structure
increases)
2). Navigate the array - i.e. index to specific element in the array

mtia

Use realloc() to create and extend a dynamic buffer. You can access
individual elements as you would with a normal array. Here's an
untested example:

#include <stdlib.h>

typedef struct {...} SomeStruct;

SomeStruct **extendArray(SomeStruct **arr, size_t *cursize, size_t
extent)
{
SomeStruct **tmp;

/**
* realloc() will return NULL on failure, address of the
* first element in the buffer on success. In order to
* extend the buffer, realloc() may move it to a
* different location, so the base address may change.
*/
tmp = realloc(arr, sizeof arr[0] * (*cursize + extent));
if (tmp)
{
arr = tmp;
*cursize += extent;
}
return arr;
}

int main(void)
{
/**
* Make sure array pointer is initially NULL
*/
SomeStruct **myArr = NULL;
size_ t arrSize = 0;
size_t i;

/**
* Initially allocate 20 elements to myArr
*/
myArr = extendArray(myArr, &arrSize, 20);
if (arrSize != 20)
{
/**
* Allocation failed; fatal error
*/
return EXIT_FAILURE;
}

for (i = 0; i < arrSize; i++)
{
/**
* do something with myArr[i]
*/
}

/**
* Extend array by another 5 elements
*/
myArr = extendArray(myArr, &arrSize, 5);
if (arrSize != 25)
{
/**
* Attempt to extend existing array failed.
*/
return EXIT_FAILURE;
}

for (i = 20; i < arrSize; i++)
{
/**
* do something with myArr[i]
*/
}

return EXIT_SUCCESS;
}

There are probably a hundred better ways to indicate success or
failure, and you'll want to add some intelligent error handling, but
that should give you the basic idea.


many thanks John

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Robert S | last post by:
I am trying to use POST to transfer data to another page. When I do this, '.' characters get converted to"_". For example: #index.html: <form action="test.php" method="post"> <input...
2
by: Katit | last post by:
Hello, I searched archives and all pointing to use of VB6 objects from C# I want something different. 1. How can I build expandable platform? Let's say DLL's will have common set of methods...
7
by: bookon | last post by:
I was running into the System.Drawing.Image.FromStream "parameter is not valid" on some of the images I was retrieving from a blob column in Sql Server. I thought there were corrupt images as...
11
by: gg9h0st | last post by:
i saw a code refactorying onload event listener window.onloadListeners=new Array(); window.addOnLoadListener=function(listener) { window.onloadListeners=listener; } why declare the...
18
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an...
0
by: No.23 | last post by:
#!/usr/bin/env python import libgmail from time import gmtime, strftime fp = open('/tmp/python.list', 'w') ga = libgmail.GmailAccount("No.0023@gmail.com", "mypass") ga.login() result =...
3
by: cketcham | last post by:
All, I have the following code: use 5.010; use LWP::UserAgent; use URI::URL; use LWP::Debug qw(+); my $browser = LWP::UserAgent->new();
11
by: 200dogz | last post by:
Hi guys, I'm making an expandable table which currently layers of <table>s are used to contain the table rows in order to show/hide them. This works, but having to format columns of the child...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.