marvinm
asked on
dynamic array of static sized strings
I want to create a dynamic array of strings, each string of size 100. I know I can use char **ppMyArray, allocate the number I want, and then allocate each pointer to size 100. My question: Is there a way to create the array without the need to loop through and allocate each pointer to size 100?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you put the string in a structure and dynamically create them?
typedef struct tagSTRING_THING
{
char strData[100];
} STRING_THING;
typedef struct tagSTRING_THING
{
char strData[100];
} STRING_THING;
ASKER
a = (char (*)[100])malloc() seems to work
If I declare a char **, then I need to malloc num_elements * sizeof (char *), and then for each one num_elements*100
do I need to allocate num_elements * ((sizeof(char *)+100)?
Thank You
(this is the last part of my question)
If I declare a char **, then I need to malloc num_elements * sizeof (char *), and then for each one num_elements*100
do I need to allocate num_elements * ((sizeof(char *)+100)?
Thank You
(this is the last part of my question)
Almost. The declaration:
char (*a)[100];
is a pointer to an array of 100 characters. So what you are allocating is multiples of arrays of 100 characters. So the malloc would be:
a=(char (*)[100])malloc(numelement s*sizeof(c har)*100);
For calloc it would be:
a=(char (*)[100])calloc(numelement s,sizeof(c har)*100);
This is, for completeness, in contrast to:
char **a;
With this declaration a is a pointer to a pointer. Semantically you wind up treating them the same. But the memory layout is different. In the first case you have a single pointer to a block of memory that represents some number of 100 byte arrays. In the second case you have a pointer to an array (of some size) of pointers. Each of the elements in the "second" array then points to individual arrays of 100 bytes. So in the first case a location is found with a pointer dereference, followed by a 2 calculations (find the block, then calculate where the desired array is, then calculate the position of the element). In the second case there are two dereferences followed and two calculation (find the second array, calculate which pointer you want, dereference that, then calculate where the element is).
char (*a)[100];
is a pointer to an array of 100 characters. So what you are allocating is multiples of arrays of 100 characters. So the malloc would be:
a=(char (*)[100])malloc(numelement
For calloc it would be:
a=(char (*)[100])calloc(numelement
This is, for completeness, in contrast to:
char **a;
With this declaration a is a pointer to a pointer. Semantically you wind up treating them the same. But the memory layout is different. In the first case you have a single pointer to a block of memory that represents some number of 100 byte arrays. In the second case you have a pointer to an array (of some size) of pointers. Each of the elements in the "second" array then points to individual arrays of 100 bytes. So in the first case a location is found with a pointer dereference, followed by a 2 calculations (find the block, then calculate where the desired array is, then calculate the position of the element). In the second case there are two dereferences followed and two calculation (find the second array, calculate which pointer you want, dereference that, then calculate where the element is).
P.S. note that the difference in the memory layout at the root of why they have to be malloced differently. In the first case there is a single block of memory, so a single malloc or calloc can fill the requirements.
But in the second case there are separate memory blocks for each array, plus one for the pointer array. Therefore you have to do all the mallocs or callocs separately.
But in the second case there are separate memory blocks for each array, plus one for the pointer array. Therefore you have to do all the mallocs or callocs separately.
ASKER
Great!
Thank You
Thank You
ASKER
warning: assignment from incompatible pointer type?
Thanks!