Sunday, July 12, 2009

Trivial C++ string question?

In certain compilers ( possibly only dev- C++, I didn't try on anything else), it is possible to declare a string variable with including %26lt;string%26gt; header file. Why is this possible?


Could anyone inform me about the uses of the string header file and whether or not it is required to declare strings variables. In addition,I have heard that a string is just an array of char, so what is the difference between declaring an array of char and declaring a string. Is it more advisable to use an array of char rather than using a string? And what are the possible advantages and disadvantages of doing so?

Trivial C++ string question?
when u include the header %26lt; string%26gt; n then use a string class to declare a string variable then u can use the string modification functions available withthat particular header ...


When u declare a string as an array of characters u dont get this functionality ...


the difference between declaring a string as an object of string class this is like


#include%26lt;string%26gt;


.


.


.


string sample = "blah blah";


here the sample is a simple object of type "string" which is actually a class whose declaration n definition is in the header file. so ther might be some function available with the class definition that u can use like


length()


substring(..)


reverse()


toUpper()


etc etc


where as declaring like :





char sample[] .......


indicates that the sample is an array of type char.


No functions available by default unless u use some other header with char array modificating functions ..


here sample has an extra character /0 indicating the end of string which is not the case if u use an object of string class.


I hope i explained it sufficiently for u .
Reply:1. Dev C++ is an IDE, not a compiler. I think it uses mingw as its compiler (at least wxDevCpp does)





Look into wxDevCPP. http://wxdsgn.sourceforge.net/





2.You need the string header to use the string data type.





3. The string data type is better because you don't have to write a bunch of string manipulation functions and because the string data type prevents buffer overruns because it knows its own size.
Reply:In C++ there is no data type called string


all you have is char array only





moreover string header file is used to manipulate some string operations to find string length, to find reverse of the string and to compare two strings ..etc





without using the string header files also you can perform string related operations in C++ (but you have to write methods for that)





I think now you are clear
Reply:As explained by "blithe-lad" there are lot of additional functionalities with string object.





Though there is no string class in C++, it is part of stl (stadard template library) and widely used.





This is very handy especially when you do lot of string manipulations with the data. If your compiler supports stl it will have the support for string.


No comments:

Post a Comment