Author | Message | Time |
---|---|---|
Imperceptus | 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. [code] 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; } [/code] | September 23, 2005, 2:09 AM |
rabbit | *split will be a single array. [code]#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; }[/code] Completely untested! I'm not sure if it works, but I hope it helps :\ | September 23, 2005, 2:31 AM |
K | Returning 4096*sizeof(string) bytes of data sounds like a really bad idea, especially if its up to the caller to free the result. https://davnit.net/bnet/vL/phpbbs/index.php?topic=2843.0 | September 23, 2005, 2:43 AM |
Imperceptus | thanks for your help. | September 23, 2005, 3:52 AM |
JoeTheOdd | Excuse my idioticity, but isn't a char already an array? [code]char char_array[] = "THIS IS CHAR\x00";[/code] char_array[0] == "T" char_array[1] == "H" char_array[2] == "I" char_array[3] == "S" and so on and so forth. | October 23, 2005, 6:19 AM |
shout | [quote author=Joe link=topic=12884.msg131800#msg131800 date=1130048375] Excuse my idioticity, but isn't a char already an array? [/quote] No. A char is a signed byte. | October 23, 2005, 2:36 PM |
rabbit | [quote author=Joe link=topic=12884.msg131800#msg131800 date=1130048375] Excuse my idioticity, but isn't a char already an array? [code]char char_array[] = "THIS IS CHAR\x00";[/code] char_array[0] == "T" char_array[1] == "H" char_array[2] == "I" char_array[3] == "S" and so on and so forth. [/quote]That's a string, although you declared it (improperly, and unindexed) as a string. The way to do what you're trying is [tt]char *char_array[] = "THIS IS A STRING\x00";[/tt] | October 23, 2005, 2:54 PM |
Kp | [quote author=Shout link=topic=12884.msg131804#msg131804 date=1130078165] [quote author=Joe link=topic=12884.msg131800#msg131800 date=1130048375] Excuse my idioticity, but isn't a char already an array? [/quote] No. A char is a signed byte. [/quote] 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. | October 23, 2005, 3:26 PM |
K | [quote author=rabbit link=topic=12884.msg131805#msg131805 date=1130079296] [quote author=Joe link=topic=12884.msg131800#msg131800 date=1130048375] Excuse my idioticity, but isn't a char already an array? [code]char char_array[] = "THIS IS CHAR\x00";[/code] char_array[0] == "T" char_array[1] == "H" char_array[2] == "I" char_array[3] == "S" and so on and so forth. [/quote]That's a string, although you declared it (improperly, and unindexed) as a string. The way to do what you're trying is [tt]char *char_array[] = "THIS IS A STRING\x00";[/tt] [/quote] 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. | October 23, 2005, 6:18 PM |
rabbit | 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. | October 23, 2005, 8:17 PM |
Kp | [quote author=rabbit link=topic=12884.msg131815#msg131815 date=1130098623] If he's writing in C or is using calls to libraries written in C, strings are required to have that null terminator. [/quote] 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? | October 23, 2005, 8:30 PM |
rabbit | Strings...character arrays are different........well, I might be wrong. | October 23, 2005, 10:37 PM |
Arta | 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++. | October 24, 2005, 8:29 AM |