Help With Array of Pointers to chars
Geoffrey Rogers
grogers at convex.com
Wed Apr 3 11:59:05 AEST 1991
In article <1991Apr2.193849.17442 at daffy.cs.wisc.edu> jansa at cat27.cs.wisc.edu (Dean Jansa) writes:
>What is the fastest way, ( read efficent use of memory and quick ) to
>transfer strings from a struct i.e:
>
> struct something
> {
> string[10];
> string2[10];
> .
> };
>into a array of pointers to chars:
> char *myarray[10];
>
>I need to malloc each pointer to char to be able to hold 10 chars in this
>example then do a strcpy. Any easier ways out there??
How about:
char *myarray[10], *p;
struct something x;
int i;
p = myarray = (char *) malloc(sizeof(x));
for ( p += 10, i = 1; i < 10; ++i, p += 10 )
myarray[i] = p;
memcpy((void *) myarray[0], (void *) &x, sizeof(x));
If you have copy the strings into the array of pointers. Otherwise do:
myarray[0] = x.string;
myarray[1] = x.string2;
" "
" "
" "
myarray[9] = x.string9;
If you know you are not going to overwrite x.
For very short loops it is almost best to unroll the loop yourself, because
not all compilers will do this.
+------------------------------------+---------------------------------+
| Geoffrey C. Rogers | "Whose brain did you get?" |
| grogers at convex.com | "Abbie Normal!" |
| {sun,uunet,uiucdcs}!convex!grogers | |
+------------------------------------+---------------------------------+
More information about the Comp.lang.c
mailing list