|
From: Santiago P. C. <San...@yd...> - 2002-07-30 08:22:53
|
Hi Ramana,
Please have in mind that you are managing 'strings' (let's say: objects of
string class) and not char arrays...
Your assertion should be right ONLY if you were managing char arrays, but
you are handling string objects.
Please have a look at the following code (compile & run it):
<CODE>
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
using std::cout;
string myVariable1 = "Some content";
string myVariable2 = "Some different content";
myVariable1.resize( myVariable2.size() );
cout << "Variable No. 1 content: " << myVariable1 << endl;
cout << "Variable No. 2 content: " << myVariable2 << endl;
// The size of the string object may be different than the number of
chars stored
if ( myVariable1.size() != sizeof(myVariable1) )
{
cout << "This is an error!!!" << endl;
}
return 0;
}
</CODE>
Just take a closer look to string methods (like size(), length() and so on)
and use them instead of sizeof !!! ;o)
Hope this helps!!!
Regards, Santy
-----Original Message-----
From: Ramana Kumar [mailto:ra...@my...]
Sent: Tuesday, July 30, 2002 9:40 AM
To: C++
Subject: [Dev-C++] sizeof array
I have the feeling that I am missing something fundamental in my
understanding of the sizeof operator or multidimensional arrays when I get
these results:
--------snip----------
String Substitutions[][2] = //String is a user-defined class
{
"I'M","I AM",
"YOU'RE","YOU ARE",
"HE'S","HE IS",
"SHE'S","SHE IS",
"IT'S","IT IS",
"WE'RE","WE ARE",
"THEY'RE","THEY ARE"
};
bool SomeFunc(String s)
{
s.Substitute(Substitutions); //pass multidimensional array to member
function }
--------snip----------
--------snip----------
void String::Substitute(String sub[][2]) //passed Substitutions here {
String ss[][2] = //exact replica of Substitutions {
"I'M","I AM",
"YOU'RE","YOU ARE",
"HE'S","HE IS",
"SHE'S","SHE IS",
"IT'S","IT IS",
"WE'RE","WE ARE",
"THEY'RE","THEY ARE"
};
if(sizeof(ss)!=sizeof(sub)) //sizeof identical arrays
{
MessageBox(NULL,"Error","Error",MB_OK);
}
}
--------snip----------
Compiles fine. The MessageBox always appears when running, indicating that
the size of those two identical arrays are different! Obviously something to
do with pointers and passing arrays to functions or something like this is
messing up the operation of the sizeof operator. Anyone have any ideas?
-------------------------------------------------------
This sf.net email is sponsored by: Dice - The leading online job board for
high-tech professionals. Search and apply for tech jobs today!
http://seeker.dice.com/seeker.epl?rel_code=31
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
--------------------------------------------------------------------------------------
This message and any files transmitted with it are confidential and intended solely
for the use of the individual or entity to whom they are addressed. No confidentiality
or privilege is waived or lost by any wrong transmission.
If you have received this message in error, please immediately destroy it and kindly
notify the sender by reply email.
You must not, directly or indirectly, use, disclose, distribute, print, or copy any
part of this message if you are not the intended recipient. Opinions, conclusions and
other information in this message that do not relate to the official business of
Ydilo Advanced Voice Solutions, S.A. shall be understood as neither given nor endorsed by it.
--------------------------------------------------------------------------------------
|