I am learning from tutorials online how to program in c++. I am trying to make a function like the vb function split. I think I have made a good attempt, when I call the function it tells me im trying to convert an int to a char*. I have looked through my function and I dont see an integer anywhere.
heres what I have so far.
char *split(char* strdata, char *delimeter, long limit = -1)
{
char* *start;
int pos = 0;
char *strcomb;
int arrlimit = 0;
for (int x = 0; arrlimit < limit; x++ ){
if (strdata[x] == *delimeter) {
if (x == 0 ) {
*start++;
for (int i = pos - 1; i != x; i++){
strcomb = strcomb + strdata[i];
}
start[x] = strcomb;
arrlimit++;
}
}
}
return *start;
}
*split will be a single array.
#include <string>
#include <stdio.h>
...
string *split(string strData, string delim, long limit = -1)
{
long lim = 1;
string *buffer = new string[4096];
sprintf(buffer[0], "%s", substr(strData.substr(strData.find(delim, 0))));
while(lim < limit && limit != -1)
sprintf(buffer[lim], "%s", substr(buffer[lim - 1].substr(buffer[lim - 1]..find(delim, 0))));
return buffer;
}
Completely untested! I'm not sure if it works, but I hope it helps :\
Returning 4096*sizeof(string) bytes of data sounds like a really bad idea, especially if its up to the caller to free the result.
http://forum.valhallalegends.com/phpbbs/index.php?topic=2843.0
thanks for your help.
Excuse my idioticity, but isn't a char already an array?
char char_array[] = "THIS IS CHAR\x00";
char_array[0] == "T"
char_array[1] == "H"
char_array[2] == "I"
char_array[3] == "S"
and so on and so forth.
Quote from: Joe on October 23, 2005, 01:19 AM
Excuse my idioticity, but isn't a char already an array?
No. A char is a signed byte.
Quote from: Joe on October 23, 2005, 01:19 AM
Excuse my idioticity, but isn't a char already an array?
char char_array[] = "THIS IS CHAR\x00";
char_array[0] == "T"
char_array[1] == "H"
char_array[2] == "I"
char_array[3] == "S"
and so on and so forth.
That's a string, although you declared it (improperly, and unindexed) as a string. The way to do what you're trying is
char *char_array[] = "THIS IS A STRING\x00";
Quote from: Shout on October 23, 2005, 09:36 AM
Quote from: Joe on October 23, 2005, 01:19 AM
Excuse my idioticity, but isn't a char already an array?
No. A char is a signed byte.
Not always. Sometimes it's an unsigned byte. This inconsistency traps many programmers who make assumptions about the signedness of chars. Whenever sign is a concern, be explicit: use int8_t and uint8_t.
Quote from: rabbit on October 23, 2005, 09:54 AM
Quote from: Joe on October 23, 2005, 01:19 AM
Excuse my idioticity, but isn't a char already an array?
char char_array[] = "THIS IS CHAR\x00";
char_array[0] == "T"
char_array[1] == "H"
char_array[2] == "I"
char_array[3] == "S"
and so on and so forth.
That's a string, although you declared it (improperly, and unindexed) as a string. The way to do what you're trying is char *char_array[] = "THIS IS A STRING\x00";
Joe's declaration is better than yours. Looks like you've declared a pointer to an array of characters, whereas his code is (correctly) an array of characters.
Also, I don't know of any compiler out there that requires you to explicitly null terminate a string constant.
If he's writing in C or is using calls to libraries written in C, strings are required to have that null terminator. The way Joe declared his array, as by my experience, makes life a pain in comparisons.
Quote from: rabbit on October 23, 2005, 03:17 PM
If he's writing in C or is using calls to libraries written in C, strings are required to have that null terminator.
No, strings are required to have
a null terminator. The compiler is smart enough to put one on there for you, since it is so often required. Thus,
you do not need to
explicitly specify a null terminator. Clear?
Strings...character arrays are different........well, I might be wrong.
You are. A char array is a string is a char array. They're the same thing. The closest thing to a string type in C++ is the stl string class. There's no native string type in C or C++.