Debugging string function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2004
Posts: 11
Reputation: sinB is an unknown quantity at this point 
Solved Threads: 0
sinB sinB is offline Offline
Newbie Poster

Debugging string function

 
-1
  #1
Mar 22nd, 2005
Hi,
If any of you know JavaScript, you will know about the split() member function of a string. In it's simplest form, it splits the string that you call it from by the char argument and returns an array. I found this very useful, so I tried to write it in C++. Here is the code:

  1. char **split(char *split_string,char splitby) {
  2. int i,j,l,sslen=strlen(split_string);
  3.  
  4. /*
  5. 'k' is a global variable that I can access to tell me how long was the array returned when the string was processed.
  6. An example of this functions use might be
  7.  
  8. char **splitup=split(mystring,' ');
  9.  
  10. */
  11.  
  12. char str_arr[sslen/2][sslen];
  13.  
  14. for(i=0; i<sslen; i++) {
  15. if(split_string[i]==splitby) {
  16. k++;
  17.  
  18. for(j=i-1,l=0; j>0; j--,l++) {
  19. if(split_string[j]==splitby) break;
  20.  
  21. str_arr[k][l]=split_string[j];
  22. }
  23. }
  24. }
  25.  
  26. if(k=0) strcpy(str_arr[0],split_string);
  27.  
  28.  
  29. return str_arr;
  30. }

Why does my compiler (Turbo c++ v 3) return errors?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,438
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 249
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Debugging string function

 
0
  #2
Mar 22nd, 2005
Why does my compiler (Turbo c++ v 3) return errors?
Posting the error messages is often helpful.
char str_arr[sslen/2][sslen];
You cannot declare an array with non-constant dimensions.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 11
Reputation: sinB is an unknown quantity at this point 
Solved Threads: 0
sinB sinB is offline Offline
Newbie Poster

Re: Debugging string function

 
0
  #3
Mar 22nd, 2005
I removed the non-constant array dimensions. Now it "Cannot convert 'char[300] *' to 'char * *' in function split(char *,char)".

My previous error messages:
  1. Error C:\SINBAD\CPP\LIBRARY\xstring.h 55: Constant expression required in function split(char *,char)
  2. Error C:\SINBAD\CPP\LIBRARY\xstring.h 55: Constant expression required in function split(char *,char)
  3. Error C:\SINBAD\CPP\LIBRARY\xstring.h 72: Cannot convert 'char[1] *' to 'char * *' in function split(char *,char)
  4. Error testing.cpp 24: Lvalue required in function main(int,char * *)

Ignore the line numbers, xstring.h has a little more in it.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,438
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 249
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Debugging string function

 
0
  #4
Mar 22nd, 2005
Originally Posted by sinB
Now it "Cannot convert 'char[300] *' to 'char * *' in function split(char *,char)".
Very true. A pointer to a pointer is not the same as an array of pointers.

If I had a better idea of what the expected input and output of the function were, I could give better help. *Sorry*

Are you familiar with the STL?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: Debugging string function

 
0
  #5
Mar 22nd, 2005
Originally Posted by Dave Sinkula
Very true. A pointer to a pointer
That's like pointers in dynamic memory allocation
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC