Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Moe

Simple string question...

This topic is 6124 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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)

Share this post


Link to post
Share on other sites
Have a look at strdup().
Beware that the memory it allocates must be free()ed and not deleted.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
why not use the string class? It is very easy to use, don''t make things hard on yourself.

Share this post


Link to post
Share on other sites
  #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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!