On 05/22/10 08:17 AM, Ankit wrote:
Please:
a) stop top-posting and
b) take the time to distil a compilable example the demonstrates your
problem.
I would suggest the following:
1) When you are writing C++, stick with C++ and use string. Not only
because it is better and safer than char*, but for consistency of your
code and the ability to have compilers better optimize your code, and
most importantly to make better use of external libraries which are
also C++ and not C directed.
2) IF you insist on passing a pointer to any function with the intent
of passing an array (char or otherwise) make sure you pass the length
of the array with it and use it to dissolve any ambiguity. Different
compilers have different symantics to analyze arrays for which no size
parameters is passed. In case of char array, some compilers would
translate it as "keep reading until you hit a \n" which the people
mentioned above. Other compilers may interpret it differently, and
hence the minor non-portability issues.
3) For example, see these two different constructors of string:
string ( const char * s, size_t n );
Content is initialized to a copy of the string formed by the first
n
characters in the array of characters pointed by s.
string ( const char * s );
Content is initialized to a copy of the string formed by the null-
terminated character sequence (C string) pointed by s. The length of
the caracter sequence is determined by the first occurrence of a null
character (as determined by traits.length(s)). This version can be
used to initialize a string object using a string literal constant.
See how the first resolves the ambiguity completely, but the second
relies on that null character.
Unfortunately, your code does not help a lot, but it would do you
best, not for this particular error, but for your program in general,
to move to C++ directed syntax and symantics. To keep your huge code
base in your case, try to change the definitions of char* to string,
and then if you have used the char* in ALOT of places, just use
yourString.c_str() to handle backward compatibility.
David Karam