• Welcome to Valhalla Legends Archive.
 

splitting char to array

Started by Imperceptus, September 22, 2005, 09:09 PM

Previous topic - Next topic

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.


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;     
}
Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.

rabbit

*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 :\
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

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.

http://forum.valhallalegends.com/phpbbs/index.php?topic=2843.0

Imperceptus

Quote from: Hazard on August 07, 2003, 03:15 PM
Highlight your entire code. Press the delete key. Start over again using Cuphead's CSB tutorial and work your way from their rather than raping code from downloaded sources meant purely for learning purposes. If this does not fix the problem, uninstall Visual Basic and get a new hobby. I suggest Cricket.

Joe[x86]

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: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

shout

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.

rabbit

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";
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Kp

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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

K

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.

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.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

Kp

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?
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

rabbit

Strings...character arrays are different........well, I might be wrong.
Grif: Yeah, and the people in the red states are mad because the people in the blue states are mean to them and want them to pay money for roads and schools instead of cool things like NASCAR and shotguns.  Also, there's something about ketchup in there.

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