pointer array question

B

Brett

Sorry for the rookie question....

If I have a pointer array (for example):

char *colors[] = {
"blue",
"green"
};

and I want to add yellow to this array later in my code....how would I do
that?

Thanks!
 
H

hari4063

You probably need dinamic memory data, like lists. Use google for "list
tutorial", there is tones list tutorials.
 
C

CBFalconer

Brett said:
If I have a pointer array (for example):

char *colors[] = {
"blue",
"green"
};

and I want to add yellow to this array later in my code....
how would I do that?

You can't. There is no room. You told the compiler to set the
array size from the initialization, so your code is the same as:

char *colors[2] = {"blue", "green"};

i.e. an array of pointers to unalterable strings. Whenever you
define such a thing you would be well advised to add a const, as
in:

const char *colors[2] = {"blue", "green"};

which will make for clearer error messages if you forget.
 
A

Al Bowers

Brett said:
Sorry for the rookie question....

If I have a pointer array (for example):

char *colors[] = {
"blue",
"green"
};

and I want to add yellow to this array later in my code....how would I do
that?

I'm curious. Why would you want to add it later?
I would include it in the original declaration.

If you wish, you can dynamic allocate the array. This would
be costly in efficiency because you are doing this just to
control one small area of memory "yellow".

Example:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct COLORS
{
const char **color;
size_t cnt;
} COLORS;

int AddColor(COLORS *p,const char *s);
void FreeColors(COLORS *p);

int main(void)
{
size_t i;
COLORS my = {NULL};

AddColor(&my,"blue");
AddColor(&my,"green");

puts("\tColors in array");
for(i = 0; i < my.cnt;i++)
printf("%u. %s\n",i+1,my.color);
/* now add a color */
AddColor(&my,"yellow");
puts("\n Attempt to add the color yellow\n");
puts("\tColors in array");
for(i = 0; i < my.cnt;i++)
printf("%u. %s\n",i+1,my.color);
FreeColors(&my);
return 0;
}

int AddColor(COLORS *p,const char *s)
{
char **tmp;

if((tmp = realloc((char **)p->color,
(p->cnt+1)*(sizeof *tmp))) == NULL)
return 0;
p->color = tmp;
if((p->color[p->cnt] = malloc(strlen(s)+1)) == NULL)
return 0;
strcpy((char *)p->color[p->cnt++],s);
return 1;
}

void FreeColors(COLORS *p)
{
size_t i;

for(i = 0; i < p->cnt; i++) free((char *)p->color);
free((char **)p->color);
p->color = NULL;
p->cnt = 0;
return;
}

--
Al Bowers
Tampa, Fl USA
mailto: (e-mail address removed) (remove the x to send email)
http://www.geocities.com/abowers822/
 
B

Barry Schwarz

Sorry for the rookie question....

If I have a pointer array (for example):

char *colors[] = {
"blue",
"green"
};

and I want to add yellow to this array later in my code....how would I do
that?

If you know that you will want to assign values to additional elements
of the array during execution, size the array with an appropriate
quantity of excess elements.

char *colors[10] = {"blue", "green"};
...
colors[2] = "yellow";


<<Remove the del for email>>
 
J

Joe Wright

Brett said:
Sorry for the rookie question....

If I have a pointer array (for example):

char *colors[] = {
"blue",
"green"
};

and I want to add yellow to this array later in my code....how would I do
that?

Thanks!

You can't. This is a case for dynamic allocation of arrays. Maybe
something like ...

char **color = malloc(3 * sizeof *color);
color[0] = "blue";
color[1] = "green";
color[2] = NULL;

The NULL is there to calculate the length of *color lest we forget.

int len;
for (len = 0; color[len], ++len) ;

Now len == 2. To add a color we can ...

color = realloc(color, (len + 2) * sizeof *color);
color[len] = "yellow";
color[len+1] = NULL;
 

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,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top