473,381 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,381 software developers and data experts.

Function Return Char[]

Hi All,

I just want to write a function return char array,but I cant. I am not
good at C++ 's concept since I used to use Java before. Would you please
help me to fix that ? Thanks..

Best Regards,
David
Jul 19 '05 #1
5 15867
Thanks a lot.
"Kevin Goodsell" <us*********************@neverbox.com> ?????
news:3f4edce1@shknews01...
david wrote:
Hi All,

I just want to write a function return char array,but I cant. I am not good at C++ 's concept since I used to use Java before. Would you please help me to fix that ? Thanks..


Arrays are not "First class citizens" in C++. They cannot be passed to a
function or returned from a function (not directly, anyway). You have a
few options.

1. You can return a pointer to an array.
2. You can return some kind of class object containing an array.
3. You can return a container, such as std::vector.

I'd recommend option 3.

Option 1 has a few gotchas, such as 1) you'd damn well better not return
a pointer to a local automatic array, 2) if you return a static array
you might have problems because a second call to the function will
modify the result from an earlier call, and 3) if you return a dynamic
array, the caller has to be sure to free that array.

Option 2 is not very elegant or flexible.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2
david wrote:
Hi All,

I just want to write a function return char array,but I cant. I am not
good at C++ 's concept since I used to use Java before. Would you please
help me to fix that ? Thanks..


Arrays are not "First class citizens" in C++. They cannot be passed to a
function or returned from a function (not directly, anyway). You have a
few options.

1. You can return a pointer to an array.
2. You can return some kind of class object containing an array.
3. You can return a container, such as std::vector.

I'd recommend option 3.

Option 1 has a few gotchas, such as 1) you'd damn well better not return
a pointer to a local automatic array, 2) if you return a static array
you might have problems because a second call to the function will
modify the result from an earlier call, and 3) if you return a dynamic
array, the caller has to be sure to free that array.

Option 2 is not very elegant or flexible.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3
A

Hi All,

I just want to write a function return char array,but I cant. I am not good at C++ 's concept since I used to use Java before. Would you please help me to fix that ? Thanks..

Either return a std::string (preferable) or a pointer to an array of char
that has been either statically or dynamically allocated.


#include <string>

class Foo{
public:
string func1();
char[] func2();
}
...


Jul 19 '05 #4
A wrote:
Hi All,

I just want to write a function return char array,but I cant. I am

not
good at C++ 's concept since I used to use Java before. Would you

please
help me to fix that ? Thanks..


Either return a std::string (preferable) or a pointer to an array of char
that has been either statically or dynamically allocated.

#include <string>

class Foo{
public:
string func1();

OK.
char[] func2(); No, you can't do that. Please reread my quoted response.
}
..

Cheers,
--ag
--
Artie Gold -- Austin, Texas

Jul 19 '05 #5
david wrote:
I just want to write a function return char array,but I cant. I am not
good at C++ 's concept since I used to use Java before. Would you please
help me to fix that ? Thanks..


Even in Java, you can't return an array, can you? You can return a
std::vector, that corresponds to Vector in Java.

--
John L. Fjellstad

A: Top posting!
Q: What is the most irritating thing on Usenet?
Jul 19 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: muser | last post by:
In the following function there is an access violation error, some memory can't be read. A week ago this code did compile. Can anyone possibly tell me why my compiler is unable to read part of...
1
by: Charlie | last post by:
I have the first part of the project done but I'm having difficulty deciding how to add an evaluate function to this program. The program asks the user to enter a function in infix notation and...
6
by: aurgathor | last post by:
Howdy, How do I pass some function a generic comparison function? I figured out one non-generic case, but since this code got parameter declarations in two places, it's obviously not generic....
35
by: michael.casey | last post by:
The purpose of this post is to obtain the communities opinion of the usefulness, efficiency, and most importantly the correctness of this small piece of code. I thank everyone in advance for your...
2
by: Fernando Barsoba | last post by:
Dear all, I have been posting about a problem trying to encrypt certain data using HMAC-SHA1 functions. I posted that my problem was solved, but unfortunately, I was being overly optimistic. I...
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
9
by: cmk128 | last post by:
Hi Why put * in front of the function name in the declaration? int (*write)(struct inode *, struct file *, const char *, int); thanks from Peter (cmk128@hotmail.com)
18
by: ben.carbery | last post by:
Hi, I have just written a simple program to get me started in C that calculates the number of days since your birthdate. One thing that confuses me about the program (even though it works) is...
4
by: default | last post by:
Hello All. I was looking around for a function like strtok() which would tokenize on the complete list of delimiters, rather than tokenize on *any* of the delimiters in the group. I ended up...
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.