Simple string question...
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
exactly!
rather than declaring it like you did, declare it like this:
then you have to allocate memory for it:
basically, it can now hold mylength-1 characters.
when you are done though, you have to free the memory:
hope that helps.
--- krez (krezisback@aol.com)
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++ stylestringtofile = 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++ styledelete[] stringtofile;
hope that helps.
--- krez (krezisback@aol.com)
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)
you could also use STL strings (which i know nothing about really, but everyone else is always talking about them).
--- krez (krezisback@aol.com)
why not use the string class? It is very easy to use, don''t make things hard on yourself.
#include <iostream>#include <string> // string class header; no .h extensionusing 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!
quote:Original post by krez
sizeof(char)
sizeof is defined as the size in char''s, so...
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.
Anyway, it''s real easy to use. Only problem is that win32 uses a lot of LPSTRs.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement