hi all,
I just got this freaky kind of doubt....I have the following piece of code, - int main()
-
{
-
int Val= 10, *ptr;
-
ptr = &Val;
-
-
TestFn(&Val);
-
-
TestPtr(ptr);
-
-
return 0;
-
}
-
-
void TestFn(int *i)
-
{
-
....does some operations
-
}
-
-
void TestPtr(int *j)
-
{
-
....does some other operations
-
}
Technically speaking for the first function (TestFn) I am passing the address of the variable Val. So I can call this function as pass by reference function.
But when it comes to the second function (TestPtr), what i am doing is the following, the address of the Val variable is assigned to Ptr. So, for second function we are just sending the address directly as input parameter. You people get my point right, the function's input parameter is a pointer and the input is also a pointer (with the address of another variable). So this just an equivalent to pass by value (not with values, but with addresses in pointer variables)
Can I call this as pass by value then ?
Am I clear or do I need to be more descriptive....
10 2232 JosAH 11,448
Recognized Expert MVP
C uses pass by value only; even when you pass a pointer as a parameter, the
value of that pointer is passed to the function. C++ also has a pass by reference
mechanism but that's not what you're using in your example code.
kind regards,
Jos
Thanks for the reply Jos.
So you mean here that if I use the same code in C++, then it can be used as pass by reference also. If yes, can you just brief me about the mechanism how it is handled or some location where I can look for answers....
Thanks for the reply Jos.
So you mean here that if I use the same code in C++, then it can be used as pass by reference also. If yes, can you just brief me about the mechanism how it is handled or some location where I can look for answers....
Hi,
In C, pointers also occupy some place in the memory to store the address.
In C++ there is something called reference type. This is just like giving an alias/ new name to the variable. -
int main()
-
{
-
...
-
int i=5;
-
...
-
int &j=i;
-
...
-
}
-
Here j does is not allocated with memory. Instead it refers to the same location of i. Consider the following pass-by-reference function call in C++: -
int main()
-
{
-
..
-
int i=5;
-
...
-
call function(i);
-
...
-
}
-
void function(int &j)
-
{
-
...
-
}
-
I hope this was helpful.
Regards,
Pradeep
AdrianH 1,251
Recognized Expert Top Contributor
Pradeep response is extremely close. The only thing that wasn’t mentioned is that a reference can take up memory. However, the compiler's optimiser may realise it is not necessary and not bother.
Adrian
Pradeep response is extremely close. The only thing that wasn’t mentioned is that a reference can take up memory. However, the compiler's optimiser may realise it is not necessary and not bother.
Adrian
Hi,
I read it somewhere that its unspecified whether references require memory or not. Could anybody throw some light on this?
Thanks,
Pradeep
AdrianH 1,251
Recognized Expert Top Contributor
Hi,
I read it somewhere that its unspecified whether references require memory or not. Could anybody throw some light on this?
Thanks,
Pradeep
I don't know what the standard says, but if the compiler can remove the need for allocating memory, it will try to. But there are times that this may not be possible.
It is probably unspecified so that the compiler manufacturers can do as much or as little as possible.
Adrian
I don't know what the standard says, but if the compiler can remove the need for allocating memory, it will try to. But there are times that this may not be possible.
It is probably unspecified so that the compiler manufacturers can do as much or as little as possible.
Adrian
Ok. Thank You Adrain.
Regards,
Pradeep
AdrianH 1,251
Recognized Expert Top Contributor
Ok. Thank You Adrain.
Regards,
Pradeep
No prob. Glad to help.
Adrian
Hey thanks to adrian and pradeep for making thing clear.....
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Randell D. |
last post by:
Folks,
I've asked this before but never got any response but its important and I
thought I'd pitch it again (hopefully a bit clearer)...
One can pass data from one function to another either by passing a copy, or
passing by reference.
My understanding of passing by reference (putting the & before a variable
during the function declaration) means that the original data is used.
|
by: Sam |
last post by:
Hi I'm learning to code with C++ and wrote some very simple code. I
think it's consistent with every rule but always got compiling errors
that I don't understand.
The code include 5 files as following, delimited by //////:
////////////////pose.h
#ifndef pose_h
#define pose_h
#include "point.h"
|
by: Zlatko Matiæ |
last post by:
Hi everybody!
Recently I was struggling with client/server issues in MS Access/PostgreSQL
combination.
Although Access is intuitive and easy to use desktop database solution, many
problems appear when someone is trying to use it as front-end for real
server database systems such as PostgreSQL or MySQL.
One of these problems is regarding pass-through queries and parameters.
I wanted to have all the code on client, while executing it on...
|
by: QQ |
last post by:
SpreadsheetCell(const string& initialValue);
I don't understand what & means here?
Thank you very much!
A C++ beginner
|
by: Martin Jørgensen |
last post by:
Hi,
Problem:
========
Some of my output functions are beginning to take pretty many
arguments... I mean.... We're talking about 10-15 arguments :-)
So I thought to myself, perhaps this is beginning to get out of hands if
I continue to put in extra functionality in the (file-writing)
output-functions....
| |
by: LaundroMat |
last post by:
Suppose I have this function:
def f(var=1):
return var*2
What value do I have to pass to f() if I want it to evaluate var to 1?
I know that f() will return 2, but what if I absolutely want to pass a
value to f()? "None" doesn't seem to work..
Thanks in advance.
|
by: Pat |
last post by:
Hi,
I've run into a strange problem, but one that seems like it might be
fairly common.
I have a single base class, from which several other classes are derived.
To keep the example simple, the base class is "Animal" and the derived
classes are "Cat," "Dog," and "Horse."
The base class has a pure virtual method:
|
by: Trent |
last post by:
Here is the error while using Visual Studio 2005
Error 1 error LNK2019: unresolved external symbol "void __cdecl
print(int,int,int,int,int,int,int,int)" (?print@@YAXHHHHHHHH@Z) referenced
in function _main assign2.obj
Thanks a lot !
Here is the code:
|
by: venkatagmail |
last post by:
I have problem understanding pass by value and pass by reference and
want to how how they are or appear in the memory:
I had to get my basics right again. I create an array and try all
possible ways of passing an array. In the following code, fun1(int
a1) - same as fun1(int* a1) - where both are of the type passed by
reference. Inside this function, another pointer a1 is created whose
address &a1 is different from that of the passed...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |