944,068 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4830
  • C++ RSS
Mar 22nd, 2005
-1

Debugging string function

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sinB is offline Offline
11 posts
since Dec 2004
Mar 22nd, 2005
0

Re: Debugging string function

Quote ...
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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 22nd, 2005
0

Re: Debugging string function

I removed the non-constant array dimensions. Now it "Cannot convert 'char[300] *' to 'char * *' in function split(char *,char)".

My previous error messages:
C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sinB is offline Offline
11 posts
since Dec 2004
Mar 22nd, 2005
0

Re: Debugging string function

Quote 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?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Mar 22nd, 2005
0

Re: Debugging string function

Quote originally posted by Dave Sinkula ...
Very true. A pointer to a pointer
That's like pointers in dynamic memory allocation
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Emergency help, my assignment is due in 24hours
Next Thread in C++ Forum Timeline: having trouble with arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC