Archived

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

c++ strings

This topic is 5018 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

way does this piece of code not work: #include <iostream.h> #include <conio.h> char test[255]; int main() { test = "hello world"; return 0; } this is the error: C:\Documents and Settings\Administrator\Desktop\c++\string test.cpp(9) : error C2440: ''='' : cannot convert from ''char [12]'' to ''char [255]'' is there any way to create a string with like a max number of character that can be recorded?

Share this post


Link to post
Share on other sites
  1. <iostream.h> is a non-standard header file. It is not guaranteed to be available, and there are no guarantees as to its contents. Do not use it. Use <iostream> instead (example below).

  2. A null-terminated character array (char *) is interpreted as a string by many C (and consequently, C++) functions, but it is not intrinsically a string. Thus, it has no inherent methods or operators and doesn't obey expected string semantics. For string behavior, use <string> (example below).

  3. A null-terminated character array in C, being an array, has no concept of its own dimensions and thus can not perform bounds checking. This makes all C arrays and memory sequences vulnerable to a series of attacks called buffer exploits, in which carefully constructed strings are passed to functions such that they overwrite adjacent memory and later cause the instructions placed in that memory to be executed (without validation). Another reason not to use char * for string representation and manipulation.

    If you choose to, however, be aware of the length-restricted variants of C string functions: strncmp, strncpy, etc. The n stands for number of character elements.

#include <iostream>
#include <string>
 
int main()
{
// Elements in the new, standard header files reside in a namespace called std.
// This namespace can be introduced in entirety or individual elements can be imported via
// the using declaration.
using namespace std;
 
string test;
test = "Hello, World!"
cout << test << endl;
return 0;
}


[Edit: Formatting error.]

[edited by - Oluseyi on March 21, 2004 3:46:19 PM]

Share this post


Link to post
Share on other sites
I''m pretty sure you can put the using namespace std; outside of the function, up at the top by the include statements as well, thus making it globally included in all your functions rather than just your main function.

Share this post


Link to post
Share on other sites
quote:
Original post by save yourself
I''m pretty sure you can put the using namespace std; outside of the function, up at the top by the include statements as well, thus making it globally included in all your functions rather than just your main function.


You can, but that just defeats the whole purpose of having namespaces in the first place.

Share this post


Link to post
Share on other sites
It doesn''t necessarily defeat the whole purpose. If you''re working in a large source file, prepending the namespace to every variable may get a bit annoying. And if there are any name conflicts, you just fully qualify the variable name.

Share this post


Link to post
Share on other sites
quote:
Original post by psykr
And if there are any name conflicts, you just fully qualify the variable name.
Which defeats the purpose of namespaces (by taking a "try and see" approach). My method introduces the namespace within a known scope where I can verify that there is no collision. Since it is a compile-time evaluation, I can do it for every function that needs it (or not) with no performance effect.

Share this post


Link to post
Share on other sites