#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!
Goto comp.lang.c++
It's a C++ feature, Off-Topic here.
:
: #include <stdio.h>
It is "call by reference ". You can change value of the parameter (num) but
not the address of it......
> 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
No, but you can pass a reference to a variable to a called function in
C.
- Kevin.
No, you can't.
-Daniel
(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
Thanks, that's exactly what I was referring to.
- Kevin.
> 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
However, saying that func(int&num), is a feature of C,
is just plain wrong.
--
pete
>
> #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.
>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
> 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!"
>> 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 <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.