|
-
September 19th, 2005, 02:54 AM
#1
return char pointer: pls help.
Hello all,
My simple function will get a substring in a provided string that recognized by starting and ending string.
Example: GetSubString("abcdefghijklmn", // source string
"def", // starting string
"klm" // ending string) will return "ghij"
My code like this:
char* GetSubString(char* cSource, char* cStart, char* cEnd)
{
char* cTemp;
cTemp = strstr(cSource, cStart);
int iStart = cTemp - cSource + strlen(cStart);
cTemp = strstr(cSource, cEnd);
int iEnd = cTemp - cSource - 1;
for (int i=iStart; i<=iFinish; i++)
cTemp[i-iStart] = cSource[i];
char* cResult = (char*) malloc(strlen(cTemp));
cResult = cTemp;
return cResult;
}
The length of substring I got is always more than the desired. I dont understand why it is? Have anybody point it out?
Thank you very much.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|