jansa@cat27.cs.wisc.edu (Dean Jansa) (04/03/91)
I a quick Question for all of you...
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];
string3[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??
Thanks
C ya...
c60b-1eq@web-1e.berkeley.edu (Noam Mendelson) (04/03/91)
In article <1991Apr2.193849.17442@daffy.cs.wisc.edu> jansa@cat27.cs.wisc.edu (Dean Jansa) writes: >I a quick Question for all of you... >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]; > string3[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?? If you can be assured that the original strings will remain in memory in the same location, you can do: myarray[0]=struct.string; myarray[1]=struct.string2; myarray[2]=struct.string3; etc. The strings won't be copied, but the myarray[] pointers will point to the correct strings. If all of the strings in the struct are the same size, you can use something like #define MYARRAY(i)=*( (char *)( &struct + i * SIZE ) ) where SIZE in your example struct would be 11. If you actually need to _copy_ the string from one location to another, I can't see any way around using strcpy(). +==========================================================================+ | Noam Mendelson ..!agate!ucbvax!web!c60b-1eq | "I haven't lost my mind, | | c60b-1eq@web.Berkeley.EDU | it's backed up on tape | | University of California at Berkeley | somewhere." |
jinx@milton.u.washington.edu (Everyone Knows JINX) (04/03/91)
jansa@cat27.cs.wisc.edu (Dean Jansa) writes: >I a quick Question for all of you... >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]; > string3[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?? ummm... Let me geth this straight. You want an ARRAY of 10 POINTERS to char. (char *myarray[10]) You have a STRUCT with 10 char ARRAYS. struct { char string[10][10]; } mystruct can you just do a: for(i=0; i != 10; i++) myarray[i] = mystruct.string[i]; or something like that? ( I slightly modified your original structure so taht it would work in a loop, but I think you get the idea...) You don't need to malloc space for an array of pointers. -- jinx@milton.u.washington.edu Disclaimer: OFS.*
grogers@convex.com (Geoffrey Rogers) (04/03/91)
In article <1991Apr2.193849.17442@daffy.cs.wisc.edu> jansa@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@convex.com | "Abbie Normal!" | | {sun,uunet,uiucdcs}!convex!grogers | | +------------------------------------+---------------------------------+