Valhalla Legends Archive

Programming => General Programming => C/C++ Programming => Topic started by: Imperceptus on September 22, 2005, 09:09 PM

Title: splitting char to array
Post by: Imperceptus on September 22, 2005, 09:09 PM
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;     
}
Title: Re: splitting char to array
Post by: rabbit on September 22, 2005, 09:31 PM
*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 :\
Title: Re: splitting char to array
Post by: K on September 22, 2005, 09:43 PM
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
Title: Re: splitting char to array
Post by: Imperceptus on September 22, 2005, 10:52 PM
thanks for your help.
Title: Re: splitting char to array
Post by: Joe[x86] 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.
Title: Re: splitting char to array
Post by: 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.
Title: Re: splitting char to array
Post by: 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";
Title: Char sign
Post by: Kp on October 23, 2005, 10:26 AM
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.
Title: Re: splitting char to array
Post by: K on October 23, 2005, 01:18 PM
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.
Title: Re: splitting char to array
Post by: 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.  The way Joe declared his array, as by my experience, makes life a pain in comparisons.
Title: Null terminated character arrays
Post by: Kp on October 23, 2005, 03:30 PM
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?
Title: Re: splitting char to array
Post by: rabbit on October 23, 2005, 05:37 PM
Strings...character arrays are different........well, I might be wrong.
Title: Re: splitting char to array
Post by: Arta on October 24, 2005, 03:29 AM
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++.