void function(int &a) { }

G

Greg P.

I saw this code in C++ but when tried to C causes an error:

You are trying to pass by reference, which is C++ specific. You can try two
things, if you don't want to make a copy of 'a' (which an int is hardly
anything to worry about):

void function(int *a) { *a = 5; }

or just remove the ampersand altogether

I won't get into references here as this newsgroup is for the C standard,
not C++
 
C

Chris Dollin

- Chameleon - said:
I saw this code in C++ but when tried to C causes an error:
-------------
void function(int &a) { a = 5; }
-------------
with this, passed in "function" the "a" pointer instead of "a" value, BUT
inside "function" I have access to "a" value not with "*a" but with "a".

Why this is not working in C?

Because C++ isn't the same language as C, so there's no obvious
reason why a C++ feature would work in C.

If you want to use C, learn C.
 
D

Dan Pop

In said:
I saw this code in C++ but when tried to C causes an error:
-------------
void function(int &a) { a = 5; }
-------------
with this, passed in "function" the "a" pointer instead of "a" value, BUT
inside "function" I have access to "a" value not with "*a" but with "a".

Why this is not working in C?

Why isn't Fortran syntax working in C?
Why isn't Pascal syntax working in C?
Why isn't Perl syntax working in C?
And so on, and so on, and so on...

Next time, try engaging your brain before posting a question. It doesn't
(or, at least, shouldn't) hurt.

Dan
 
M

Martin Ambuhl

Greg P. wrote:

I won't get into references here as this newsgroup is for the C standard,
not C++

No, this newsgroup is for standard C. The newsgroup for the C standard
is
 
G

Greg P.

| No, this newsgroup is for standard C. The newsgroup for the C standard
| is That's what i meant, give a guy some slack =)
 
G

Greg P.

| Because it's nonsense. '(int &a)' is a syntax error. If you want to do
| this in C, write in C instead of using the pointless syntactic sugar
| introduced in C++ for people too stupid to write C:
I half agree with that statement as I find templates a break from thousands
of lines of code (which are only C++). But I also find that references are a
lazy way of lacking pointer knowledge.
 
G

Guest

The sytnax is not C language syntax. You yourself pointed out that it's
from
a different language. Here are some other things that won't work:

$a =~ s/\d+/$1+1/eg;
0 -72 -72 0 0 72 72 0 4 72 72 moveto { rlineto } repeat stroke
if (a instanceof java.lang.String) System.out.println((String) a);

... and so on ...

ha ha! Ok! I thought that maybe it is standard C but I was wrong!
Thanks!
 
G

Guest

I saw this code in C++ but when tried to C causes an error:
Why isn't Fortran syntax working in C?
Why isn't Pascal syntax working in C?
Why isn't Perl syntax working in C?
And so on, and so on, and so on...

Its clearly now ;-)
Next time, try engaging your brain before posting a question. It doesn't
(or, at least, shouldn't) hurt.

be cool
 
S

Slartibartfast

Greg P. said:
| Because it's nonsense. '(int &a)' is a syntax error. If you want to do
| this in C, write in C instead of using the pointless syntactic sugar
| introduced in C++ for people too stupid to write C:
I half agree with that statement as I find templates a break from thousands
of lines of code (which are only C++). But I also find that references are a
lazy way of lacking pointer knowledge.

Yes, of course C++ reference syntax is nonsense in a C program, but to describe it as "pointless syntactic sugar" or "a lazy way of
lacking pointer knowledge [sic]" demonstrates your ignorance of C++.
 
G

goose

I saw this code in C++ but when tried to C causes an error:
-------------
void function(int &a) { a = 5; }
-------------
with this, passed in "function" the "a" pointer instead of "a" value, BUT
inside "function" I have access to "a" value not with "*a" but with "a".

Why this is not working in C?

because C is not C++.

C does not support pass-by-reference function calls. presumably,
C++ does.

the above in C would be
void function (int *a) { *a = 5; }

hth
goose,
 
M

Martin Ambuhl

Greg said:
| Because it's nonsense. '(int &a)' is a syntax error. If you want to do
| this in C, write in C instead of using the pointless syntactic sugar
| introduced in C++ for people too stupid to write C:
I half agree with that statement as I find templates a break from thousands
of lines of code (which are only C++). But I also find that references are a
lazy way of lacking pointer knowledge.

While templates can be useful, there were no templates in the posted
code to which I responded. The only C++ syntactic sugar in evidence was
the so-called "pass-by-reference."
 
B

bd

I saw this code in C++ but when tried to C causes an error:
-------------
void function(int &a) { a = 5; }
-------------
with this, passed in "function" the "a" pointer instead of "a" value, BUT
inside "function" I have access to "a" value not with "*a" but with "a".

Why this is not working in C?

Because C isn't C++.
 
C

Cousin Ricky

I saw this code in C++ but when tried to C causes an error:
-------------
void function(int &a) { a = 5; }
-------------
with this, passed in "function" the "a" pointer instead of "a" value, BUT
inside "function" I have access to "a" value not with "*a" but with "a".

Why this is not working in C?

Perhaps because C is not C++. Just a guess.
 
D

Default User

Martijn said:
Why all these offensive responses? It is a legitimate question which only
Greg seems to take this seriously. All those "witty" responses are more
cluttering than the original post.

How do you figure that it is a legitimate question? Even the most
cursory of research would have revealed that C doesn't support
references.
I guess it is true what they say: "There are no stupid questions, only
stupid answers" :)

Besides stupid questions and stupid answers there are stupid statements.




Brian Rodenborn
 
G

Guest

ha ha! Ok! I thought that maybe it is standard C but I was wrong!
If you are going to do work in C, get a book and learn it. Otherwise you
just waste your time and ours.

I work in C for 2,5 years
I learned C day by day from other C sources
from help pages
and from this newsgroup
Thanks!
 
D

Default User

I work in C for 2,5 years
I learned C day by day from other C sources
from help pages
and from this newsgroup


This merely emphasizes my point. If you have been studying using your
methods for over two years and have achieved no better success than you
demonstrate, GET A DAMN BOOK!

Picking up bits and pieces is no way to learn a language, you need a
systematic approach.


Like I said, you waste your time and ours.



Brian Rodenborn
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,951
Messages
2,570,113
Members
46,698
Latest member
alexxx

Latest Threads

Top