Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!ucbvax!agate!web-1e.berkeley.edu!c60b-1eq From: c60b-1eq@web-1e.berkeley.edu (Noam Mendelson) Newsgroups: comp.lang.c Subject: Re: Help With Array of Pointers to chars Message-ID: <1991Apr2.230903.29225@agate.berkeley.edu> Date: 2 Apr 91 23:09:03 GMT References: <1991Apr2.193849.17442@daffy.cs.wisc.edu> Sender: usenet@agate.berkeley.edu (USENET Administrator) Organization: University of California, Berkeley Lines: 42 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." |