473,769 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

difference b/w call by reference and call by pointer

Hi,

i am a c++ programmer,
now i want to learn programming in c also.

so can anybody explain me the difference b/w call by reference and
call by
pointer (with example if possible).

Feb 22 '07 #1
10 16673
In article <11************ **********@j27g 2000cwj.googleg roups.com>,
ravi <dc**********@g mail.comwrote:
>Hi,

i am a c++ programmer,
now i want to learn programming in c also.

so can anybody explain me the difference b/w call by reference and
call by
pointer (with example if possible).
It is much like the difference between the Easter Bunny and George Bush.

One exists and the other doesn't (and more's the pity).

I.e., what you call "call by pointer" is the poor substitute for what
we'd really like (call by reference).

Feb 22 '07 #2
On 22 Feb, 12:09, "ravi" <dceravigu...@g mail.comwrote:
Hi,

i am a c++ programmer,
now i want to learn programming in c also.

so can anybody explain me the difference b/w call by reference and
call by pointer (with example if possible).
It's probably more helpful to understand that C has only one calling
convention - call by value.

When a function in C is called, arguments are passed to it "by value"
- a simple approach (not strictly accurate, but workable in my
opinion) is to regard this as creating copies of the data passed, and
passing these. Changes made in the called function are not visible to
the calling function.

$ cat ravi.c
#include <stdlib.h>
#include <stdio.h>
void ravi(int a);
int main(void) {
int a = 10;
printf("in main before calling ravi - a is %d\n",a);
ravi(a);
printf("in main after calling ravi - a is %d\n",a);
return EXIT_SUCCESS;
}
void ravi(int a) {
printf("\tin ravi - a is initially %d\n",a);
a += 20;
printf("\tin ravi - a is now %d\n",a);
}

$ ./ravi
in main before calling ravi - a is 10
in ravi - a is initially 10
in ravi - a is now 30
in main after calling ravi - a is 10

If data in the calling function is to be modified by the called
function, then you must explicitly (except in one special case, which
I'll talk about in a moment) use pointers.

$ cat ravi2.c
#include <stdlib.h>
#include <stdio.h>
void ravi(int *a); /* function now takes a pointer to int */
int main(void) {
int a = 10;
printf("in main before calling ravi - a is %d\n",a);
ravi(&a); /* pass the address of a rather than its value */
printf("in main after calling ravi - a is %d\n",a);
return EXIT_SUCCESS;
}
void ravi(int *a) {
printf("\tin ravi - a is initially %d\n",*a);
*a += 20;
printf("\tin ravi - a is now %d\n",*a);
}

$ ./ravi2
in main before calling ravi - a is 10
in ravi - a is initially 10
in ravi - a is now 30
in main after calling ravi - a is 30

The exception (sort of) is when you pass an array as an argument to a
function.

In C, the arrayname's value is the address of the first element of the
array, so passing the array allows the called function direct access
to the array.

Feb 22 '07 #3
"ravi" <dc**********@g mail.comwrote:
i am a c++ programmer,
now i want to learn programming in c also.

so can anybody explain me the difference b/w call by reference and
call by pointer (with example if possible).
There's no such thing as call by reference in C. There's no such thing
as call by pointer at all.

All parameters are passed by value in C. To pass a pointer in C, you
pass a pointer, TTBOMK just as you would in C++.

As usual (*sigh*) reading the FAQ before you posted here would have made
you a wiser man: <http://c-faq.com/ptrs/passbyref.html> .

Richard
Feb 22 '07 #4
On Feb 22, 2:09 pm, "ravi" <dceravigu...@g mail.comwrote:
Hi,

i am a c++ programmer,
now i want to learn programming in c also.

so can anybody explain me the difference b/w call by reference and
call by
pointer (with example if possible).

Feb 22 '07 #5
ravi wrote:
Hi,

i am a c++ programmer,
Judging by your lack of comprehension of basic concepts in the other
thread, you might want to change the word 'programmer' to 'student'.
now i want to learn programming in c also.
Right. So pickup a copy of "The C Programming Language" by Kernighan &
Ritchie and start doing the exercises.
so can anybody explain me the difference b/w call by reference and
call by pointer (with example if possible).
Why do you start a new thread for the same beaten topic? There's no
call by reference and no "call by pointer", whatever that is, in C. C
only supports call by value semantics. However by using pointers we
can, for most purposes, simulate what should be called as call by
reference. Note the word simulate, since that's what it is.

Feb 22 '07 #6
In article <11************ **********@j27g 2000cwj.googleg roups.com>,
ravi <dc**********@g mail.comwrote:
>so can anybody explain me the difference b/w call by reference and
call by
pointer (with example if possible).
C provides only call by value. Passing a pointer is a way to
simulate call-by-reference, though you have to explicitly dereference
the pointer wherever you use it in the called function.

In a C-like call-by-reference language, you could do

void foo(int x)
{
x = x+1;
}

int main(void)
{
int a;
foo(a);
...
}

and the value of a would be changed. In real C, you would do:

void foo(int *x) /* the parameter type is a pointer to int */
{
*x = *x + 1; /* to access the int, you must dereference the pointer */
}

int main(void)
{
int a;
foo(&a); /* you pass a pointer to a rather than a itself */
...
}

[Array names are converted to pointers in almost all contexts so
arrays are effectively call-by-reference for most purposes.]

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Feb 22 '07 #7
ma**********@po box.com writes:
[...]
The exception (sort of) is when you pass an array as an argument to a
function.

In C, the arrayname's value is the address of the first element of the
array, so passing the array allows the called function direct access
to the array.
Strong emphasis on the "sort of".

In fact, you cannot pass an array as an argument to a function. An
expression of array type (such as the name of an array object) is
implicitly *converted*, in most contexts, to a pointer to the array's
first element. (The exceptions are when the array expression is the
operand of a unary "&" or "sizeof" operator, or is a string literal in
an initializer used to initialize an array object.)

This conversion really has nothing to do with function calls; an
argument in a function call is just one of the many contexts in which
the conversion takes place. You're not passing an array; you're
passing a pointer value (through which the actual array can be
accessed).

For more information, see section 6 of the comp.lang.c FAQ,
<http://www.c-faq.com>.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 22 '07 #8
"santosh" <sa*********@gm ail.comwrites:
[...]
Why do you start a new thread for the same beaten topic? There's no
call by reference and no "call by pointer", whatever that is, in C. C
only supports call by value semantics. However by using pointers we
can, for most purposes, simulate what should be called as call by
reference. Note the word simulate, since that's what it is.
To say the same thing in another way, call-by-reference *as a language
feature* does not exist in C; it only has call-by-value. But
call-by-reference *as a programming technique* can easily be
implemented in C; it's implemented by passing a pointer value.
(Whether you call this "simulation " is a matter of taste, I suppose.)

It's similar to the question of whether C has linked lists. There's
no built-in language feature that directly implements linked lists,
but you can easily create linked lists using the lower-level building
blocks (pointers, structures, malloc, free) that the language
provides. I wouldn't necessarily refer to this as "simulating " linked
lists, unless I was thinking in terms of some other language that
provides linked lists as a built-in language feature.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 22 '07 #9
On 22 Feb 2007 04:09:09 -0800, "ravi" <dc**********@g mail.comwrote
in comp.lang.c:
Hi,

i am a c++ programmer,
now i want to learn programming in c also.

so can anybody explain me the difference b/w call by reference and
call by
pointer (with example if possible).
It's pretty simple in C, because there is no such thing as call by
reference.

All arguments to C functions are passed by value. If you want a C
function to be able to modify some object in the caller, you pass a
pointer to that object.

So there is no example of call by reference in C, and passing a
pointer looks just the same in C as it does in C++.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Feb 23 '07 #10

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

Similar topics

9
1880
by: jlopes | last post by:
I'm looking at the differences between these to forms and see no difference in their use. When accessed through a derived class. class ABase { public: virtual void filter(){ /* some code */ } }; class D_of_ABase : public ABase
35
10792
by: hasho | last post by:
Why is "call by address" faster than "call by value"?
5
8732
by: Chris | last post by:
Hi, I don't get the difference between a struct and a class ! ok, I know that a struct is a value type, the other a reference type, I understand the technical differences between both, but conceptually speaking : when do I define something as 'struct' and when as 'class' ? for example : if I want to represent a 'Time' thing, containing : - data members : hours, mins, secs
5
6541
by: Kurt Van Campenhout | last post by:
Hi, I am trying to get/set Terminal server information in the active directory on a windows 2000 domain. Since the ADSI calls for TS don't work until W2K3, I need to do it myself. I'm fairly new to VB.NET, so I need some help. Here is a code snippit :
13
26596
by: mitchellpal | last post by:
i am really having a hard time trying to differentiate the two..........i mean.....anyone got a better idea how each occurs?
14
7185
by: Vols | last post by:
If the people ask what is the different between pointer and reference, what is the brief and good answer? I say " pointer could point to NULL, but there is no null reference", What is your opinion? Vol
22
4189
by: Nehil | last post by:
Does C follow call by value convention or call by reference? i see that there is nothing like reference in C standard but it is referenced. still, what should be the answer for the above question?
7
2021
by: thomas | last post by:
I’m just writing a program which uses the queue stl type. Queue<packet* queue_; Queue<packet& queue_;
11
1499
by: ram kishore | last post by:
What is the difference between (int *) fun() and int *fun() ? What is the advantage of pointers to functions?
0
9420
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,...
0
10205
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9984
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,...
0
9851
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 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...
0
8863
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, 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...
1
7401
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 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...
0
5293
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...
1
3949
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.