|
-
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.
-
September 19th, 2005, 03:05 AM
#2
Re: return char pointer: pls help.
The way of returning a string back to main is simple,i didn't checked your program but in case of using malloc use new.perform your calcualtion on your string find out substring and return it to main nothing else you have to do.
Code:
char *Check(char *ppc);
int main(int argc, char* argv[])
{
char *ppc = new char[255];
char *ppc1;
ppc1 =Check(ppc);
printf("%s",ppc1);
delete []ppc;
return 0;
}
char *Check(char *ppc)
{
ppc = "hello hi";
return ppc;
}
Just try to Find your Substring and Return back to main.
Last edited by humptydumpty; September 19th, 2005 at 03:13 AM.
-
September 19th, 2005, 03:14 AM
#3
Re: return char pointer: pls help.
humptydumpty,
Your code is wrong.
In 'Check' function, you assign to a local pointer and not to the array itself.
you should use strcpy to the array (if we are dealing with C as the
sample code the the thread's author provided)
**** **** **** **** **/**
-
September 19th, 2005, 03:20 AM
#4
Re: return char pointer: pls help.
 Originally Posted by Guysl
humptydumpty,
Your code is wrong.
In 'Check' function, you assign to a local pointer and not to the array itself.
you should use strcpy to the array (if we are dealing with C as the
sample code the the thread's author provided)
First Thing Guysl.this Question is in c++ Forum so Forget about c,Second regarding poinyter see the question Once again.Third thing where to use strcpy at the time when you want to copy some data in one string .In this case no need to strcpy.Comes Under Initialization of a pointer variable.
this example shows only how to retuen pointer from a Function to main.
That's all nothing special is there.
-
September 19th, 2005, 03:21 AM
#5
Re: return char pointer: pls help.
Bmiuritan,
I think what would fix the problem is:
int iEnd = cTemp - 1;
instead of
int iEnd = cTemp - cSource - 1;
**** **** **** **** **/**
-
September 19th, 2005, 03:22 AM
#6
Re: return char pointer: pls help.
There are a few problems with what you have done 
1) You end up modifying literal data, which causes your program to crash
2) The lack of [CODE] tags made my brain crash
3) You end up re-assigning the pointer to malloc'ed data, and then never free() it
Something we might try:
Code:
char* GetSubString(char* cSource, char* cStart, char* cEnd, char* szBuf)
{
// Get offset of the substring
char* cTempOne = strstr(cSource, cStart);
char* cTempTwo = strstr(cSource, cEnd);
cTempOne += strlen(cStart);
// Copy it into our temporary buffer
int i = 0;
for(; cTempOne != cTempTwo; i++, cTempOne++)
szBuf[i] = *cTempOne;
szBuf[i] = '\0';
return szBuf;
}
int main(int argc, char* argv[])
{
char szBuf[1024];
printf("We got: %s\n",
GetSubString("abcdefghijklmn", "def",
"klm", szBuf));
return 0;
}
>> humptydumpty, Your code is wrong.
Although what the program does is probably not intended and should have used strcpy to put hello hi into the space allocated with new[], it is functional. It's just wierd as ****...
-
September 19th, 2005, 03:29 AM
#7
Re: return char pointer: pls help.
humptydumpty,
calm down. Bmiuritan uses 'malloc','strstr' and C-Stile string (char*) therefor he is obviouslly trying to code with C.
second, please read again my previous comment.
**** **** **** **** **/**
-
September 19th, 2005, 03:32 AM
#8
Re: return char pointer: pls help.
>> this Question is in c++ Forum so Forget about c
Code:
printf("%s",ppc1); // Right humptydumpty ;)
>> In this case no need to strcpy.Comes Under Initialization of a pointer variable.
What you did I think is not what you intended.
Code:
printf("ppc in Check: %p\n", ppc); // 00420048
printf("ppc in main: %p\n", ppc); // 00860090
Those are numbers on XP. In Check(), you reassign a copy of ppc to point to literal data (~004xxxxx), while really, in main(), as you intended, ppc is a pointer to data in the free store (heap, 008xxxxx). If you merely wanted it to point to the literal data anyways, there would be no need to allocate/delete space in the first place, and you could just have:
Code:
int main(int argc, char* argv[])
{
char *ppc;
printf("%s\n", Check(ppc));
return 0;
}
-
September 19th, 2005, 03:37 AM
#9
Re: return char pointer: pls help.
I got it, thank you very much.
-
September 19th, 2005, 06:05 AM
#10
Re: return char pointer: pls help.
The main problem I see with the original code is that he calls malloc to allocate the pointer (actually allocating one byte too few) then moves the pointer instad of copying in the characters.
Another thing is that he is modifying the source string, which I'm not sure he's supposed to.
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
|