Sorry may be my description is not clear
I want data storage as
------------------------
<first block>
<char string>
<char string>
<char string>
<char string>
...
...<dynamic>
------------------------
Are all of the multiple strings the same size (or the same maximum
size)? Equivalently, given the address of the first string, can I
compute the address of the second without evaluating the actual length
of the string?
If the answer is yes, then you can point to this block of strings
(actually an array of strings) with a pointer to the first string.
char (*p1)[MAX_STRING_SIZE] = malloc(N * sizeof *p1); would allocate
space for N such strings, each occupying an array of MAX_STRING_SIZE.
p1[0] would be the first array, p1[1] the second, etc. p1 can be
reallocated to allow more strings.
If you do this, you need some way to tell when you are at the last
p
.
You either keep track of the maximum i p is valid for
elsewhere or
You set the last string to a sentinel value, such as
strcpy(p1[N-1],"");
If the strings are not the same size (often called a jagged or ragged
array), then you basically need one pointer for each string. Since
the string sizes vary, the simplest approach is a char* that points to
the start of the string. The obvious solution is a dynamic array of
such pointers. Something along the lines of
char **p2 = malloc(N * sizeof *p2);
p2[0] = &string0;
p2[1] = &string1; ...
You have the same choices as above for determining the number or
strings plus the option of setting p2[N-1] to NULL. p2 can be
reallocated to allow more strings. Furthermore, if any string is also
dynamically allocated, p2 can be reallocated as well.
snip second block
<Third block>
<char string>
<char string>
<char string>
<char string>
...
...<dynamic>
Now you have a dynamic number of blocks. You want a dynamic array
where each element is a pointer to a block.
If you chose the p1 approach above, then the pointer to the block has
type char(*)[MAX_STRING_SIZE]. The simplest approach is to change the
definition of p1 to
typedef char (*T1)[MAX_STRING_SIZE];
T1 p1 = malloc ...;
and your pointer for allocating a dynamic array of T1's is
T1 *da1 = malloc(M * sizeof *da1);
On the other hand, if you are a glutton for punishment, you can omit
the typedef and code
char (*da1)(*)[MAX_STRING_SIZE] = malloc ...
If you chose the p2 approach above, the declarations are much simpler.
char ***da2 = malloc(M * sizeof *da2);
Remove del for email