Help With Array of Pointers to chars

Noam Mendelson c60b-1eq at web-1e.berkeley.edu
Wed Apr 3 09:09:03 AEST 1991


In article <1991Apr2.193849.17442 at daffy.cs.wisc.edu> jansa at 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 at web.Berkeley.EDU                     |  it's backed up on tape  |
| University of California at Berkeley          |  somewhere."             |



More information about the Comp.lang.c mailing list