From: Eric Kaplan on
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)
From: Anders Karlsson on
On Wed, 16 Apr 2008 17:18:07 -0700, Eric Kaplan
<tobycraftse(a)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?
From: Scott McPhillips [MVP] on
"Eric Kaplan" <tobycraftse(a)yahoo.com> wrote in message
news:ub5d04t1nujulas60mnehtpdh3bn89am2n(a)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]

From: Eric Kaplan on
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 :)
From: Tom Serface on
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(a)yahoo.com> wrote in message
news:0sld04lf82j2nvb5egktsekcjd1alod5l2(a)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 :)