   alternative to char[80]
  I am using Visual C++ 2005 Express Edition and am in the process of porting a ubnch of old VC++6 code into my new project.I have a question about my old "PrintStringD3D" function I wrote, which takes a string and writes to a my own proxy vertex buffer with it.The current declaration of the function is this:void PrintStringD3D(char string[80], float p_x, float p_y)where a call would look something like this:PrintStringD3D("testing",300,300);I understand that "char string[80]" is bad, and that I'm supposed to do something with std.  What exactly am I supposed to use instead of char[]?  By the way, I have "using namespace std" defined already.   
  void PrintStringD3D(string s, float p_x, float p_y)#include <iostream>using namespace std;void PrintStringD3D(string s, float p_x, float p_y){cout << s.c_str() << " x="<<p_x << " y="<<p_y << endl;}void main(){PrintStringD3D("testing",300,300);};  void PrintStringD3D(const std::string& str, float p_x, float p_y)Within the function, you can pass str.c_str() to D3D where a const char* is needed, and use str.length() to give the length of the string.   ;  Thanks for the help, but I guess what I really want to know is if there's anything actually wrong or dangerous with the method I was using.  It's critical that I have the string as an array here, or in some form where I can easily run through all the characters.  I'm not really "passing" the string anywhere; I do all the handling myself (welcome to synth_cat's home-made text engine...)So, is char string[80] bad code or dangerous for what I'm doing here?Also...Fruny, does str.c_str() just return the last string created?  For example, if I were to write a function for loading files, could I do it like this?void LoadFile(str.c_str()){fopen(&str.c_str() . . . );} where a call to this function would look likeLoadFile("myfile");;Quote:Original post by synth_catThanks for the help, but I guess what I really want to know is if there's anything actually wrong or dangerous with the method I was using.  It's critical that I have the string as an array here, or in some form where I can easily run through all the characters.A std::string is a form where you can easily run through all the characters, as well as perform quite a few other useful operations.As for c_str(), it returns a const char* representing the contents of the string in C-style null-terminated form. Its return value can be submitted as an argument to functions that expect char pointers, e.g.:void MyFunction(const char* text) {...}std::string myString("Hello");MyFunction(myString.c_str());;Quote:Original post by synth_catThanks for the help, but I guess what I really want to know is if there's anything actually wrong or dangerous with the method I was using.  It's critical that I have the string as an array here, or in some form where I can easily run through all the characters.  I'm not really "passing" the string anywhere; I do all the handling myself (welcome to synth_cat's home-made text engine...)You can use std::string like an array by invoking the [] operator. For example, my_string[0] would return the first character of the std::string my_string. If you want to "run through" all the characters, you might want to use an iterator:for(std::string::iterator i = my_string.begin(); i != my_string.end(); ++i) {char current_character = *i;// ...}Quote:So, is char string[80] bad code or dangerous for what I'm doing here?C-style strings are susceptible to buffer overflows. STL strings are also much easier to manipulate; they have useful members like "find" and "replace", and they can be plugged into STL algorithms. STL strings can also hold any number of characters -- whereas char string[80] would only be able to store 80 letters.Quote:Also...Fruny, does str.c_str() just return the last string created?  For example, if I were to write a function for loading files, could I do it like this?*** Source Snippet Removed *** where a call to this function would look like*** Source Snippet Removed ***c_str() returns the C-string representation of a string stored in an std::string. Many functions, like the C-style file IO functions, accept C-strings as parameters, and so it is useful to be able to convert between std::strings and C-strings.   ;  std::string is defined such that you can access it like an array, i.e. iterate over it.Using a fixed length char array as a parameter is definitely bad coding.   ;  There isn't anything wrong with that code.  When you have an array as a parameter, the size of the array doesn't actualy matter, because it doesn't copy that data in.  All it does is copy the pointer in, same as if you used "char* string" instead.   ;  It would only matter for a multidimensional array.   ;  Among other things, you should read this and this (and subsequently this). And, oh, I don't know, maybe half the posts in my posting history here ;)   