[ back to toc ]


Date: 2002/02/16 12:19


I have never programed in C so my question quite simple.
I have a large string and I would like to put a part of it in a variable.
How would I do this?
if the string is "abcdefghij" how whould I make my variable be equal to
'bcd' (positions 2,3, and 4)
First of all you need a variable of the type char *

Then you have to allocate memory calling malloc of size enough to store
the sub string and the terminating zero.

You have to copy the substring calling memcpy and that is it.

char *a,*b;

a = "abcdefghij";
b = malloc(4);
if( b == NULL )exit(1);
b[3] = (char)0;


Please take your time and rate me at AllExperts.com
I ask you to do that the way you believe I deserve.
This is a feedback that I get from allexperts but
it does not affect my life in any financial way. This
is just a mental feedback.

In case you think my answer was really good and if you feel
it appropriate do not hesitate to nominate me to volunteer
of the month.

I do this job volunteer in my free time and this is all I get
as compensation.

[ back to toc ]