Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

func(int& num) ??

0 views
Skip to first unread message

chithra

unread,
Jan 23, 2003, 8:12:20 PM1/23/03
to
I'm a newbie. I'm puzzled by how func() is defined! This is from a
quiz. The code works fine(I use vc++ 6), giving an output 6,6.

#include <stdio.h>

void main()
{
int i = 5;
func(i);
printf("%d", i);
return;
}

void func(int& num)
{
printf("%d,", ++num);
}

can anyone justify the usage of & in func(int& num) ? It seems to be
equivalent to defining the function as func(int* num) and passing the
/address/ of the actual argument while calling.

Also Please suggest me a link to learn about various c compilers and
the differences between them.

Thanks!

Charles Wood

unread,
Jan 23, 2003, 8:14:29 PM1/23/03
to

"chithra" <chit...@rediffmail.com> wrote in message
news:a35a4a5e.03012...@posting.google.com...
: I'm a newbie. I'm puzzled by how func() is defined! This is from a

: quiz. The code works fine(I use vc++ 6), giving an output 6,6.

Goto comp.lang.c++

It's a C++ feature, Off-Topic here.
:
: #include <stdio.h>

KJ

unread,
Jan 23, 2003, 10:01:56 PM1/23/03
to

"chithra" <chit...@rediffmail.com> wrote in message
news:a35a4a5e.03012...@posting.google.com...

It is "call by reference ". You can change value of the parameter (num) but
not the address of it......


Ben Pfaff

unread,
Jan 23, 2003, 9:58:42 PM1/23/03
to
"KJ" <jay...@americasm01.nt.com> writes:

> It is "call by reference ". You can change value of the parameter (num) but
> not the address of it......

C doesn't have call by reference.
--
"What is appropriate for the master is not appropriate for the novice.
You must understand the Tao before transcending structure."
--The Tao of Programming

Kevin Easton

unread,
Jan 23, 2003, 10:47:06 PM1/23/03
to
Ben Pfaff <b...@cs.stanford.edu> wrote:
> "KJ" <jay...@americasm01.nt.com> writes:
>
>> It is "call by reference ". You can change value of the parameter (num) but
>> not the address of it......
>
> C doesn't have call by reference.

No, but you can pass a reference to a variable to a called function in
C.

- Kevin.

Daniel Fox

unread,
Jan 23, 2003, 11:39:38 PM1/23/03
to
Kevin Easton <ke...@pcug.org.au> wrote in
news:newscache$m3979h$r8o$1...@tomato.pcug.org.au:

No, you can't.

-Daniel

rjh

unread,
Jan 24, 2003, 12:59:01 AM1/24/03
to
Daniel Fox wrote:

(The OP clearly needs comp.lang.c++ or he needs to fix his program. Either
way, the worm can is now open.)

It is true that C does not have pass by reference. But don't overlook
6.2.5(21) which says:

"A pointer type describes an object whose value provides a reference to an
entity of the referenced type."

Thus, in C, int *p = &i; means that p's value provides a reference to i. And
we can certainly pass p's value to a function. Conclusion: in C, you cannot
pass /by/ reference, but you most certainly can pass /a/ reference.

Having said that, I find this most confusing, and I think it considerably
easier (especially for newbies, and indeed for midbies like myself) to
remember just the first of those two truths: C does not have
pass-by-reference; everything is done using pass-by-value semantics.
Remembering this makes C programs so much easier to understand.

--
Richard Heathfield : bin...@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton

Kevin Easton

unread,
Jan 24, 2003, 1:29:24 AM1/24/03
to
rjh <bin...@eton.powernet.co.uk> wrote:
> Daniel Fox wrote:
>
>> Kevin Easton <ke...@pcug.org.au> wrote in
>> news:newscache$m3979h$r8o$1...@tomato.pcug.org.au:
>>
>>> Ben Pfaff <b...@cs.stanford.edu> wrote:
>>>> "KJ" <jay...@americasm01.nt.com> writes:
>>>>
>>>>> It is "call by reference ". You can change value of the parameter
>>>>> (num) but not the address of it......
>>>>
>>>> C doesn't have call by reference.
>>>
>>> No, but you can pass a reference to a variable to a called function in
>>> C.
>>
>> No, you can't.
>
> (The OP clearly needs comp.lang.c++ or he needs to fix his program. Either
> way, the worm can is now open.)
>
> It is true that C does not have pass by reference. But don't overlook
> 6.2.5(21) which says:
>
> "A pointer type describes an object whose value provides a reference to an
> entity of the referenced type."
>
> Thus, in C, int *p = &i; means that p's value provides a reference to i. And
> we can certainly pass p's value to a function. Conclusion: in C, you cannot
> pass /by/ reference, but you most certainly can pass /a/ reference.

Thanks, that's exactly what I was referring to.

- Kevin.


Daniel Fox

unread,
Jan 24, 2003, 1:21:06 AM1/24/03
to
rjh <bin...@eton.powernet.co.uk> wrote in
news:b0qkn4$4l1$1...@helle.btinternet.com:

> Daniel Fox wrote:

[snip]

>> No, you can't.
>
> (The OP clearly needs comp.lang.c++ or he needs to fix his program.
> Either way, the worm can is now open.)
>
> It is true that C does not have pass by reference. But don't overlook
> 6.2.5(21) which says:
>
> "A pointer type describes an object whose value provides a reference
> to an entity of the referenced type."
>
> Thus, in C, int *p = &i; means that p's value provides a reference to
> i. And we can certainly pass p's value to a function. Conclusion: in
> C, you cannot pass /by/ reference, but you most certainly can pass /a/
> reference.

You can pass "an object whose value provides a reference." I would argue
this is clearly not the same thing as the concept of a passing a reference
itself (as in C++).

-Daniel

pete

unread,
Jan 24, 2003, 5:12:34 AM1/24/03
to

However, saying that func(int&num), is a feature of C,
is just plain wrong.

--
pete

Martin Ambuhl

unread,
Jan 24, 2003, 6:12:31 AM1/24/03
to
chithra wrote:
> I'm a newbie. I'm puzzled by how func() is defined! This is from a
> quiz. The code works fine(I use vc++ 6), giving an output 6,6.
^^^^^
If you use it as a C compiler (see your documentation for details), then
a question about code you compile with it may be topical here; adding
whatever is necessary to make it try to be a standard-complying C compiler
raises your odds. If you use it as a C++ compiler, you are in the wrong
place.

>
> #include <stdio.h>
>
> void main()
^^^^ illegal in all standard versions of C


> {
> int i = 5;
> func(i);

^^^^
In C89, creates an implicit declaration of
int func();
Illegal in C99.

> printf("%d", i);
> return;
> }
>
> void func(int& num)

^^^^(1) ^^^^ (2)
(1) illegal redeclaration of int func()
(2) syntax error

> {
> printf("%d,", ++num);
> }
>
> can anyone justify the usage of & in func(int& num) ?

No. There is no justification for a syntax error. Now, if you took this
to comp.lang.c++, the answer would be different.


Dan Pop

unread,
Jan 24, 2003, 8:44:12 AM1/24/03
to

>I'm a newbie. I'm puzzled by how func() is defined! This is from a
>quiz. The code works fine(I use vc++ 6), giving an output 6,6.

Did you try to compile it as a C program?

>#include <stdio.h>
>
>void main()
>{
> int i = 5;
> func(i);
> printf("%d", i);
> return;
>}
>
>void func(int& num)
>{
> printf("%d,", ++num);
>}

Let's see:

fangorn:~/tmp 44> gcc test.c
test.c: In function `main':
test.c:4: warning: return type of `main' is not `int'
test.c: At top level:
test.c:11: parse error before '&' token
test.c:12: warning: type mismatch with previous implicit declaration
test.c:6: warning: previous implicit declaration of `func'
test.c:12: warning: `func' was previously implicitly declared to return `int'
test.c: In function `func':
test.c:13: `num' undeclared (first use in this function)
test.c:13: (Each undeclared identifier is reported only once
test.c:13: for each function it appears in.)

This certainly doesn't match my idea of "works fine".

>can anyone justify the usage of & in func(int& num) ?

It's a syntax error in C. It's a C++ feature, but your program is
broken as a C++ program, too, because func is not declared before being
used (and because of the broken main definition).

>It seems to be
>equivalent to defining the function as func(int* num) and passing the
>/address/ of the actual argument while calling.

It's more than that. Note that num is NOT explicitly dereferenced in
func.

>Also Please suggest me a link to learn about various c compilers and
>the differences between them.

As a newbie, you should focus all your attention on what is common
between different compilers, not on the differences. If you use only
the common features, your programs will work everywhere.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Dan...@ifh.de

Emmanuel Delahaye

unread,
Jan 24, 2003, 12:11:34 PM1/24/03
to
In 'comp.lang.c', chit...@rediffmail.com (chithra) wrote:

> I'm a newbie. I'm puzzled by how func() is defined! This is from a
> quiz. The code works fine(I use vc++ 6), giving an output 6,6.
>
> #include <stdio.h>
>
> void main()

int main()
or
int main (void)

> {
> int i = 5;
> func(i);
> printf("%d", i);
> return;

return 0;

> }
>
> void func(int& num)

This is not C. Sounds to be C++.

news:comp.lang.c++

--
-ed- emdel at noos.fr ~]=[o
FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
C-library: http://www.dinkumware.com/manuals/reader.aspx
"Give peace a chance!"

Emmanuel Delahaye

unread,
Jan 24, 2003, 12:12:29 PM1/24/03
to
In 'comp.lang.c', "KJ" <jay...@americasm01.nt.com> wrote:

>> void func(int& num)

> It is "call by reference ". You can change value of the parameter (num) but
> not the address of it......

Not such an animal in C.

rjh

unread,
Jan 24, 2003, 5:22:07 PM1/24/03
to
Daniel Fox wrote:

> rjh <bin...@eton.powernet.co.uk> wrote in
> news:b0qkn4$4l1$1...@helle.btinternet.com:
>

<snip>


>>
>> It is true that C does not have pass by reference. But don't overlook
>> 6.2.5(21) which says:
>>
>> "A pointer type describes an object whose value provides a reference
>> to an entity of the referenced type."
>>
>> Thus, in C, int *p = &i; means that p's value provides a reference to
>> i. And we can certainly pass p's value to a function. Conclusion: in
>> C, you cannot pass /by/ reference, but you most certainly can pass /a/
>> reference.
>
> You can pass "an object whose value provides a reference."

No, because an object is a region of data storage. You can pass values, but
not regions of data storage and therefore not objects.

> I would argue
> this is clearly not the same thing as the concept of a passing a reference
> itself (as in C++).

I agree that it's not the same thing as passing a C++ reference, but we're
discussing C here. In C, you can pass a reference because a pointer type
describes an object whose value provides a reference and you can pass
values without any problem. I stress again that this is not the same as
pass-/by/-reference.

0 new messages