array of pointers to existing array of chars

B

Boris Gambon

Hi,

After reading tutorials, it's unclear to me how I can create and use
an array of pointers to an existing array of chars.

There is one big row of chars (array of chars) and I want to
dynamically allocate memory for pointers to various places in this row
of chars.

After doing that, I want to be able to use these pointers like this p
[1], p[2] for locations inside the row of chars, starting at the first
char. That should enable code like this: printf("%s", p[1]); or memcmp
in case it's not a string.

example
char data[]="text1\0another-text2\0more-text3\0"; // existing array of
chars, automatically declared

p[0] should point to position 0
p[1] should point to position 6
p[2] should point to position 20
et cetera

Questions:
How to declare a variable for the pointers?
How to malloc memory for the pointers themselves, array of pointers?
How to assign the right position to the pointers?
How to free the allocated array of pointers?

Thanks

Boris
 
M

Moi

Hi,

After reading tutorials, it's unclear to me how I can create and use an
array of pointers to an existing array of chars.

There is one big row of chars (array of chars) and I want to dynamically
allocate memory for pointers to various places in this row of chars.

After doing that, I want to be able to use these pointers like this p
[1], p[2] for locations inside the row of chars, starting at the first
char. That should enable code like this: printf("%s", p[1]); or memcmp
in case it's not a string.

example
char data[]="text1\0another-text2\0more-text3\0"; // existing array of
chars, automatically declared

p[0] should point to position 0
p[1] should point to position 6
p[2] should point to position 20
et cetera

Questions:

How to declare a variable for the pointers?

char ** pppp;

_or_ (if you don't want to malloc):
char *pppp[3] ;
How to malloc memory for thepointers themselves, array of pointers?

with malloc():

pppp = malloc (3 * sizeof *pppp);
How to assign the right position to the pointers?

By assignment:

pppp[0] = data+0;
pppp[1] = data+6;
pppp[2] = data+20;
How to free the allocated array of pointers?
With free()::

free(pppp);


HTH,
AvK
 
T

Tom St Denis

Hi,

After reading tutorials, it's unclear to me how I can create and use
an array of pointers to an existing array of chars.

There is one big row of chars (array of chars) and I want to
dynamically allocate memory for pointers to various places in this row
of chars.

After doing that, I want to be able to use these pointers like this p
[1], p[2] for locations inside the row of chars, starting at the first
char. That should enable code like this: printf("%s", p[1]); or memcmp
in case it's not a string.

example
char data[]="text1\0another-text2\0more-text3\0"; // existing array of
chars, automatically declared

p[0] should point to position 0
p[1] should point to position 6
p[2] should point to position 20
et cetera

Questions:
How to declare a variable for the pointers?

char *p; // this is a single pointer

char *p[]; // this is an array of pointers.

char *p[5]; // this is an array of 5 pointers

in the last case if you wrote p[3] = NULL, you'd set the 4th [counting
from zero] pointer to NULL
How to malloc memory for the pointers themselves, array of pointers?

char **p; // pointer to pointers of char

p = calloc(5, sizeof *p); // allocate 5 of whatever *p points to [in
this case *p would be char *]

now you can write

p[3] = NULL;

for the same effect as above.
How to assign the right position to the pointers?

Exercise left for reader, you'll need to

1. first count the # of pointers you will need
2. allocate them
3. scan for \0's and assign pointers as you encounter them
How to free the allocated array of pointers?

free(p)

In this case your pointers point to previously allocated [or attended
to] data so you don't need to free them explicitly,

but suppose you had copied them [and allocated previously] you'd have

p = calloc(5, sizeof *p);
for (x = 0; x < 5; x++) {
p[x] = calloc(1, MAXSTRLEN);
}
// do things
//...
// free up
for (x = 0; x < 5; x++) {
free(p[x]);
}
free(p);

Hope that helps,
Tom
 

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
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top