By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,958 Members | 1,156 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,958 IT Pros & Developers. It's quick & easy.

Dynamic string arrays?

P: n/a
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[i] = (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
Nov 13 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Robert wrote:
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;
}

--
Hallvard
Nov 13 '05 #2

P: n/a
Robert wrote:
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[i] = (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[i] = malloc(20 * sizeof *cpArray[i]);
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

--
Artie Gold -- Austin, Texas

Nov 13 '05 #3

P: n/a
Robert <R.****@hetnet.nl> wrote:
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[i] = (char *)malloc(20 * sizeof(char));
The cast is unnecessary and only will hide your mistake if you forget
to include <stdlib.h>. And sizeof( char ) is 1 by definition.
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
--
_ _____ _____
| ||_ _||_ _| Je***********@physik.fu-berlin.de
_ | | | | | |
| |_| | | | | | http://www.physik.fu-berlin.de/~toerring
\___/ens|_|homs|_|oerring
Nov 13 '05 #4

P: n/a

"Robert" <R.****@hetnet.nl> wrote in message
news:bj**********@reader10.wxs.nl...
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[i] = (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

--
Jeff
-je6543 at yahoo.com
Nov 13 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.