I have a function like this
=============
bool Table::Get(char* FieldName, char* FieldValue)

and I call the function like the follow
=============
while(!tbl.ISEOF())
{
char id2[300];
if(tbl.Get("program",id2))
cout<<"program:"<<id2<<endl;
else
{
tbl.GetErrorErrStr(ErrStr);
cout<<"\n"<<ErrStr<<"\n";
break;
}

}


above I passed in a char pointer (char*) - FieldValue and use it just
like a return value.

but it seem not efficient as each time i initialize a char array of
[300] characters.

but some string (FieldValue) is only like [5] char.

how to I do better for returning string? any example code?

=================

should I just return the string in regular return type like -
std::string Table::Get(char* FieldName)

Re: return string by Anders

Anders
Wed Apr 16 19:46:10 CDT 2008

On Wed, 16 Apr 2008 17:18:07 -0700, Eric Kaplan
<tobycraftse@yahoo.com> wrote:

> should I just return the string in regular return type like -
> std::string Table::Get(char* FieldName)


or even better

std::string Table::Get(const std::string &FieldName)

passing pointers to a function adds unnecesarycode since you need to
check the pointers to see if they are valid or not otherwise you can
get an exception.

passing by reference or value allows you to skip such checks except
maybe to see if - in your example - the string has zero length.

try avoid using raw char* pointers and instead use string instead or
wstring/CString for that matter.

hth/Anders.
--
A: People bitching about top-posting

> Q: What's the most annoying thing on USENET?

Re: return string by Scott

Scott
Wed Apr 16 21:39:50 CDT 2008

"Eric Kaplan" <tobycraftse@yahoo.com> wrote in message
news:ub5d04t1nujulas60mnehtpdh3bn89am2n@4ax.com...
> above I passed in a char pointer (char*) - FieldValue and use it just
> like a return value.
>
> but it seem not efficient as each time i initialize a char array of
> [300] characters.
>
> but some string (FieldValue) is only like [5] char.
>
> how to I do better for returning string? any example code?
>
> =================
>
> should I just return the string in regular return type like -
> std::string Table::Get(char* FieldName)

...And someday 300 characters will not be enough and your program will
crash. There are numerous reasons to avoid char* in programming, and it is
really unacceptable to allocate char arrays of some arbitrary length that
may or may not be sufficient. If you need code portability to other
operating systems use std::string. If you are concerned only with Windows
use CString: It is better :)

--
Scott McPhillips [VC++ MVP]


Re: return string by Eric

Eric
Wed Apr 16 23:53:57 CDT 2008

but I see quite alot of library they take (char*) as parameters.

is that for C library they use (char*)

for C++ library, most of them use std::string??


>...And someday 300 characters will not be enough and your program will
>crash. There are numerous reasons to avoid char* in programming, and it is
>really unacceptable to allocate char arrays of some arbitrary length that
>may or may not be sufficient. If you need code portability to other
>operating systems use std::string. If you are concerned only with Windows
>use CString: It is better :)

Re: return string by Tom

Tom
Thu Apr 17 08:35:29 CDT 2008

If you use CString or std::string the size won't matter since the string
will resize based on the content. I'd pass a reference into your routine
and let the function assign the value there.

char* still works, it's just not the modern way to do things. These days
most of us would either use a string object or a TCHAR array so that the
code would scale to Unicode easily.

Tom

"Eric Kaplan" <tobycraftse@yahoo.com> wrote in message
news:0sld04lf82j2nvb5egktsekcjd1alod5l2@4ax.com...
> but I see quite alot of library they take (char*) as parameters.
>
> is that for C library they use (char*)
>
> for C++ library, most of them use std::string??
>
>
>>...And someday 300 characters will not be enough and your program will
>>crash. There are numerous reasons to avoid char* in programming, and it
>>is
>>really unacceptable to allocate char arrays of some arbitrary length that
>>may or may not be sufficient. If you need code portability to other
>>operating systems use std::string. If you are concerned only with Windows
>>use CString: It is better :)



Re: return string by Scott

Scott
Thu Apr 17 08:45:51 CDT 2008

"Eric Kaplan" <tobycraftse@yahoo.com> wrote in message
news:0sld04lf82j2nvb5egktsekcjd1alod5l2@4ax.com...
> but I see quite alot of library they take (char*) as parameters.
>
> is that for C library they use (char*)
>
> for C++ library, most of them use std::string??

Yes, (char*) is the only choice available in C. Reasons to avoid it
nowadays are that it has enormous security vulnerablilities, and it cannot
handle international alphabets.

In C++ both std::string and CString can be used to pass a char* to library
functions when necessary. With a std::string you must use .c_str, with
CString you can simply pas the CString (it has an overloaded const char*
member).

--
Scott McPhillips [VC++ MVP]


Re: return string by Ben

Ben
Thu Apr 17 10:08:30 CDT 2008

Scott McPhillips [MVP] wrote:
> "Eric Kaplan" <tobycraftse@yahoo.com> wrote in message
> news:0sld04lf82j2nvb5egktsekcjd1alod5l2@4ax.com...
>> but I see quite alot of library they take (char*) as parameters.
>>
>> is that for C library they use (char*)
>>
>> for C++ library, most of them use std::string??
>
> Yes, (char*) is the only choice available in C. Reasons to avoid it
> nowadays are that it has enormous security vulnerablilities, and it
> cannot handle international alphabets.

Rather than "char* has enormous security vulnerabilities" I would say it
"encourages security vulnerabilities". There is nothing insecure about
char* when used correctly (this is in fact how std::string works
underneath), it's just more work to do so without reusing the well-tested
code in std::string.



Re: return string by Tim

Tim
Fri Apr 18 00:36:05 CDT 2008

Eric Kaplan <tobycraftse@yahoo.com> wrote:

>I have a function like this
>=============
>bool Table::Get(char* FieldName, char* FieldValue)
>
>and I call the function like the follow
>=============
>while(!tbl.ISEOF())
>{
> char id2[300];
>...
>above I passed in a char pointer (char*) - FieldValue and use it just
>like a return value.
>
>but it seem not efficient as each time i initialize a char array of
>[300] characters.

The advice you have received in this thread is excellent, but I would like
to point out that you are NOT initializing a char array here. You are
allocating space for a char array, but it's not being initialized. That
only takes one assembler instruction.
--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.