Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!uwm.edu!linac!convex!grogers From: grogers@convex.com (Geoffrey Rogers) Newsgroups: comp.lang.c Subject: Re: Help With Array of Pointers to chars Message-ID: <1991Apr03.015905.23203@convex.com> Date: 3 Apr 91 01:59:05 GMT References: <1991Apr2.193849.17442@daffy.cs.wisc.edu> Sender: news@convex.com (news access account) Organization: Convex Computer Corporation, Richardson, Tx. Lines: 47 Nntp-Posting-Host: mozart.convex.com 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 | | +------------------------------------+---------------------------------+