Skip to main content
gamedev.net
🔒 Locked

Simple string question...

Started by Moe Dec 14, 2001 at 5:35 PM 12 replies 450+ views
Original Post
Moe
Moe
I have a fairly simple question about strings. I currently have something like char stringtofile[256] = "this is obviously less than 256 characters"; I want change that so I can have more than 256 characters, without using an array. Is it done with a pointer somehow, like: LPSTR stringtofile = "this can be longer than 256 characters"; I need to use it as a parameter in a function. Moe''s site
krez
krez
exactly!

rather than declaring it like you did, declare it like this:
char* stringtofile; 

then you have to allocate memory for it:

/* c style */
stringtofile = malloc(mylength* sizeof(char));
// c++ style
stringtofile = new char[mylength];

basically, it can now hold mylength-1 characters.
when you are done though, you have to free the memory:

/* c style */
free stringtofile;
// c++ style
delete[] stringtofile;


hope that helps.

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Moe
Moe
Hmm, so its still limited in size by the amount that I allocate for it?

Moe''s site
Fruny
Fruny
Have a look at strdup().
Beware that the memory it allocates must be free()ed and not deleted.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
krez
krez
eh, you can realloc() it (i rarely do this so you''ll have to look it up yourself)...
you could also use STL strings (which i know nothing about really, but everyone else is always talking about them).

--- krez (krezisback@aol.com)
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
Oluseyi
Oluseyi
  #include <iostream>
#include <string> // string class header; no .h extension


using namespace std;
int main(int argc, char *argv[])
{
string str = "This is a string.";
cout << str;
str += " I appended a second string to it dynamically.";
cout << ''\n'' << str;
str += "\nAnd I appended a new line."
cout << str << endl;
}

Output:
This is a string.
This is a string. I appended a second string to it dynamically.
This is a string. I appended a second string to it dynamically.
And I appended a new line.


[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Beer Hunter
Beer Hunter
quote:
Original post by krez
sizeof(char)



sizeof is defined as the size in char''s, so...
Darkor
Darkor
That string class thingy is part of the STL (Standard Template Library). If you know what Templates are, you''d know how they work.

Anyway, it''s real easy to use. Only problem is that win32 uses a lot of LPSTRs.
23yrold3yrold
23yrold3yrold
Technically, class string isn''t part of the STL, not being a template class and all But it does share much of the functionality, especially in the modifying functions and iterators. Nice thing about class string is most operators and functions are overloaded six way ''til Sunday to work with char-strings, single char''s and other string objects (10 versions of insert() alone). A must-learn.

Chris Barry
Jesus saves ... the rest of you take 2d4 fire damage.
23yrold3yrold
23yrold3yrold
You sure? I''m pretty sure class string'' methods are optimized for dealing with the entire string as one object, while vectors are optimized for managing each container individually. Hmmm. Well, I guess the standard doesn''t say they can''t make string a typedef of an STL container and add new funtionality .....

Chris Barry
Jesus saves ... the rest of you take 2d4 fire damage.
Oluseyi
Oluseyi
quote:
Original post by 23yrold3yrold
I''m pretty sure class string'' methods are optimized for dealing with the entire string as one object, while vectors are optimized for managing each container individually.

?

*doesn''t get it*

Why are you using vector and string as comparable arguments? std::vector has nothing to do with std::string (which is a typedef for std::basic_string<char> btw); vector and string are are both containers and control sequences of data. That means there is little probability of similarity between them, because if there was one would simply be an adapter for the other.

Furthermore, std::string does not optimize anything - at least not in the popular Dinkumware implementation (which comes with MSVC). And according to this page, neither does the SGI implementation:
quote:
basic_string<charT, traits, Alloc>:
The types string and wstring are typedefs for, respectively, basic_string<char> and basic_string<wchar_t>.


The SGI STL site is a good resource. You can find the link in my .sig

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.