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

are strings passed by value or reference ? Output of below pgm not understood.

Are strings passed by value or reference?

I don't understand the output of below program.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream.h>
  2. void print(char *p)
  3. {
  4.     p="Pass";
  5.     cout<<"\n value is :"<<p;
  6. }
  7. void main()
  8. {
  9.     clrscr();
  10.     char *q="Best of Luck";
  11.     print(q);
  12.     cout<<"\n New Value is : "<<q;
  13.  
  14. }
The answer is displayed as follows ...
The value is : Pass
New Value is : Best of Luck

Isn't a string passed by reference? Pls explain the reason for this answer .
Jan 8 '17 #1
1 1075
weaknessforcats
9,208 Expert Mod 8TB
First, everything is passed by value. When you use q to call a function, the function makes a copy called p and assigns the address of "Pass" to p. Now you see p displayed as Pass. Then p is destroyed when the function returns. The calling function uses q in a display and you see Best of Luck.

Nothing is passed by reference unless you use the reference operator with your function argument.

As to strings, a char* is not a string. An old C convention was used to ASSUME a char* was the address of a char array whose last byte was a null (\0).

A C++ string is a class:

Expand|Select|Wrap|Line Numbers
  1. string str;
To use C++ strings you need to study the C++ Standard Library string class.
Jan 8 '17 #2

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

Similar topics

1
by: Adam Dyga | last post by:
Hi, How to create function with optional parameter passed by reference? I've tried sth like this: function (&$param=NULL) // or: function (&$param=new array()) { //... }
1
by: Marcello Pietrobon | last post by:
Hello, My background is C++ I would like to know for sure if it is possible to pass a value by reference to a function AND having it changed by the function itself def changeValue( text...
3
by: jimjim | last post by:
Hello, My question concerns as to how a pointer is passed by reference as a function argument. The following is from code taken from the MICO implementation of the CORBA specification. in...
13
by: Matthias Kaeppler | last post by:
Hi, I was wondering why library implementors often make getter functions return strings by value (copies). For example, in boost::filesystem the leaf() function returns an std::string by value....
5
by: Jerry Morton | last post by:
Hi, I'm new to C# and have run into a problem of my own making! I originally took from the documentation that as long as I didn't use the "ref" keyword in method declarations, that everything...
1
by: Bala A | last post by:
I have an ActiveX EXE. One of the methods in this component has "Recordset Object" as input parameter which has to be passed by reference. I have to call this method from C# code. From C#, how...
5
by: Aman Sura | last post by:
:confused: This is just an absurd piece of code. Can anyone please explain the reason? //a function that nulls the array passed as parameter function Nullify(arrRef) { arrRef = null; } ...
9
by: Jack | last post by:
If I don't specify "ref" in the argument list when passing an array to the callee, I am passing the array (reference) by value. But this makes me confused because it actually means a "reference" of...
1
by: fbnewtz | last post by:
I am trying to modify someone elses code that skipped out in the middle of a project and am fairly new to ASP.net. This seems like a simple insert stored proceedure on a pretty basic form. For some...
8
Dormilich
by: Dormilich | last post by:
Hi, I’m currently doing a fun project, where I stumble upon the quite annoying fact, that between some methods the arrays (they are the parameters) are passed by reference, which screws up the...
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
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?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.