[Techtalk] C Programming Question

Malcolm Tredinnick malcolm at commsecure.com.au
Wed May 29 14:32:10 EST 2002


On Wed, May 29, 2002 at 12:18:25PM +0800, Sujita Purushothaman wrote:
> Hi,
>     I'm having a problem with pointer-to-array in C.
> I have to use the structure char  *somename[ ] . I also
> need to store/assign data to the strings halfway thro the
> program. ( I know I can initialise it as :
> char  *somename[ ] = {"data1","data2"}; etc.
> At some point in the program I need to change "data1"
> to "somethingelse1" . How do I do that?

This does what you want:

	/* Reserve the space */
	int length = strlen("somethingselse1");
	somename[0] = (char *) malloc(length);
	strncpy(somename[0], "somethingelse1", length);

Note that if the initial value ("data1") was not allocated as above, you
may need to free its memory (however, in the above case, this is not
required).

Malcolm

-- 



More information about the Techtalk mailing list