CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2005
    Posts
    8

    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.

  2. #2
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Thumbs up 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.

  3. #3
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638

    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)
    **** **** **** **** **/**

  4. #4
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Thumbs up Re: return char pointer: pls help.

    Quote 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.

  5. #5
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638

    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;
    **** **** **** **** **/**

  6. #6
    Join Date
    Aug 2005
    Posts
    478

    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 ****...

  7. #7
    Join Date
    Mar 2004
    Location
    Israel
    Posts
    638

    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.
    **** **** **** **** **/**

  8. #8
    Join Date
    Aug 2005
    Posts
    478

    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;
    }

  9. #9
    Join Date
    Sep 2005
    Posts
    8

    Re: return char pointer: pls help.

    I got it, thank you very much.

  10. #10
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    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
  •  





Click Here to Expand Forum to Full Width

Featured