473,408 Members | 2,888 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,408 software developers and data experts.

Why to use return type for string function as char*?

If we are using strcpy() for copying the string. As we are passing pointers to it It will copy the string & no need to return the string .This function will finely work with return type as void then why Ritchie has used it as char* strcpy()?
Oct 6 '12 #1
4 1980
weaknessforcats
9,208 Expert Mod 8TB
Returning a value means you can use the strcpy on the right side of an assignment operator.

Maybe you like this:

Expand|Select|Wrap|Line Numbers
  1. char* arr[5;
  2.       arr[0] = strcpy(destination, str);
  3.  
  4.  
better than this:

Expand|Select|Wrap|Line Numbers
  1.       strcpy(destination, str);
  2.       arr[0] = destination;
  3.  
The second example is probably a tad slower because of the two statements.
Oct 6 '12 #2
thank you friend,
I think you are not getting my question.As we are using pointers the change will be made into the actual parameter then why to return the value?
Oct 9 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
A fuction becomes an instance of it's return value. So a function that returns an int can be used as an int. All the compiler has to do is call the function to get the value instead f you hard-coding the value.

Therefore, a function that returns a value can be used on the right side of an assignment operator. The function can be an RVAL.

Library developers, the ones who wrote strcpy, made sure that I you want to use strcpy as a RVAL you can. In fact, if you check the generated code I suspect using strcpy as an RVAL has fewer instructions generated as opposed to calling it and then assignming the value.

This is what I attempted to explain in my reply.
Oct 9 '12 #4
Thank you weaknessforcats......
Oct 15 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Andrew | last post by:
I have written two classes : a String Class based on the book " C++ in 21 days " and a GenericIpClass listed below : file GenericStringClass.h // Generic String class
13
by: Maciej | last post by:
Hallo, Can I convert type String to char* ?? I need to use fstream in my application together with openfiledialog. Thanks, Maciek
16
by: collinm | last post by:
hi i would like to return a string char *readFile(char *tmp_dir_led, char *directory, char *filename) { FILE *fp; char line; snprintf(tmp_mnt_dir_led, sizeof(tmp_mnt_dir_led),"%s/%s",
23
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
8
by: andrew.fabbro | last post by:
In a different newsgroup, I was told that a function I'd written that looked like this: void myfunc (char * somestring_ptr) should instead be void myfunc (const char * somestring_ptr) ...
10
by: Grizlyk | last post by:
1. Can abybody explain me why C++ function can not be overloaded by its return type? Return type can has priority higher than casting rules. For example: char input(); //can be compiled to name...
8
by: =?Utf-8?B?RG91Z2xhcyBNYXJxdWFyZHQ=?= | last post by:
Using VB.Net 2005. I have my interfaces and enums defined in a DLL. For example, I have this enum: Public Enum OpStatusTypes Normal InProgress Paused End Enum
1
by: poopsy | last post by:
i am writing a function where i have to return variables with datatypes text and real. wat wil be the return type of my function, text or real or..??? plz help thx
8
by: Sundhas | last post by:
I made a Webservice Operation whose return type is STRING Following is the code @WebMethod(operationName = "authorize") public String authorize(@WebParam(name = "Username") String Username) { ...
1
by: govin | last post by:
is thete need to same return type on function overloading
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.