Dynamic string arrays?

R

Robert

Hi,

How can i resize an array of strings to add more?

This is an example i just downloaded from a website:

char *cpArray[10];
int i;
for (i = 0; i < 10; i++) cpArray = (char *)malloc(20 * sizeof(char));
strcpy(cpArray[5], "A hat is on the mat");
cpArray[5][3] = 'u';

But this only resizes the lenght of the string...
How is it possible to like add another string to the array?
So that i could change the char *cpArray[10]; in runtime?

Thanks for your time,

Robert
 
H

Hallvard B Furuseth

Robert said:
How can i resize an array of strings to add more?

This is an example i just downloaded from a website:

char *cpArray[10];
(...)

#include <stdlib.h>

char **cpArray;
size_t cpArrayLen;
...
char **tmp;
size_t newLen;
cpArrayLen = 10
cpArray = malloc(cpArrayLen * sizeof(*cpArray));
if (cpArray == NULL) {
ERROR(...);
exit(EXIT_FAILURE);
}
...
newLen = cpArrayLen * 2; /* or whatever */
tmp = realloc(cpArray, newLen * sizeof(*cpArray));
if (tmp == NULL) {
ERROR(...realloc failed, cpArray unchanged...);
} else {
cpArrayLen = newLen;
cpArray = tmp;
}
 
A

Artie Gold

Robert said:
Hi,

How can i resize an array of strings to add more?

This is an example i just downloaded from a website:

char *cpArray[10];
int i;
for (i = 0; i < 10; i++) cpArray = (char *)malloc(20 * sizeof(char));


Three things here:

You don't check the return value of malloc() -- it could be fail and
be NULL.

The cast is unnecessary -- don't do it.

`sizeof(char)' is defined to be 1 by the standard, so either get rid
of it, or use the more idiomatic:

cpArray = malloc(20 * sizeof *cpArray);
strcpy(cpArray[5], "A hat is on the mat");
cpArray[5][3] = 'u';

But this only resizes the lenght of the string...
How is it possible to like add another string to the array
So that i could change the char *cpArray[10]; in runtime

You cannot.

You'd have to declare cpArray to be a pointer to pointer to char, i.e.:

char **cpArray = malloc(10 * sizeof *cpArray);

and use realloc() to make it larger (and when you do, `realloc' to a
temporary variable in case the call fails (so you can avaoid a
memory leak).

HTH,
--ag
 
J

Jens.Toerring

Robert said:
How can i resize an array of strings to add more?
This is an example i just downloaded from a website:
char *cpArray[10];
int i;
for (i = 0; i < 10; i++) cpArray = (char *)malloc(20 * sizeof(char));


The cast is unnecessary and only will hide your mistake if you forget
to include said:
strcpy(cpArray[5], "A hat is on the mat");
cpArray[5][3] = 'u';
But this only resizes the lenght of the string...

What do you mean with 'resizes the length of the string'? This copies
the second argument of strcpy() into the memory that you previously
malloced for it to the 6th element of cpArray. Luckely, the string
you copy into it just fits, one character more and you would have
written past the end of the array ;-)
How is it possible to like add another string to the array?
So that i could change the char *cpArray[10]; in runtime?

When you start of with

char *cpArray[10];

all you got is ten char pointers and you can't change the length of the
array anymore. If you want to be able to dynamically change its length
you need to first allocate memory for the char pointers, then for the
strings they are supposed to point to. Later on you can use realloc()
to change the number of pointers. E.g.

char **cpArray;

cpArray = malloc( 10 * sizeof *cpArray );

for ( i = 0; i < 10; i++ )
cpArray[ i ] = malloc( 20 );

Now you're still in the same situation as above, at least in respect
to the lengths of all the arrays involved. But if you now want to
increase the number of strings you want to store, say to 15, you
would continue with

cpArray = realloc( cpArray, 15 * sizeof *cpArray );

for ( i = 10; i < 15; i++ )
cpArray[ i ] = malloc( 20 );

And, voila, now you have 15 pointers to strings of length 20. Of course,
in a "real" program you would carefully check that your calls of malloc()
and realloc() didn't return NULL, indicating that you're running out of
memory and store the value of cpArray before calling realloc() to avoid
losing the pointer to the memory you already own in case of memory
exhaustion.
Regards, Jens
--
_ _____ _____
| ||_ _||_ _| (e-mail address removed)-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
 
J

Jeff

Robert said:
Hi,

How can i resize an array of strings to add more?

This is an example i just downloaded from a website:

char *cpArray[10];
int i;
for (i = 0; i < 10; i++) cpArray = (char *)malloc(20 * sizeof(char));
strcpy(cpArray[5], "A hat is on the mat");
cpArray[5][3] = 'u';

But this only resizes the lenght of the string...
How is it possible to like add another string to the array?
So that i could change the char *cpArray[10]; in runtime?


You cannot simply resize the cpArray[10] to add string.

1. You can use char **cpArray to store some char pointer. When you add a new
string, you should realloc the cpArray, then malloc each empty char pointer
inside the cpArray.

2. You can use a linked list for this purpose. If you want to dynamically
add (or remove) a new string the the memory, a linked list structure is more
effective.

for example:

struct node{
struct node *next;
char *sbuffer;
}
typedef struct node string_node;


If you want to write a list, I searched in google and found this
http://stsdas.stsci.edu/bps/linked_list.html

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,206
Latest member
Zenden

Latest Threads

Top