473,387 Members | 1,641 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

void function(int &a) { }

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?
Nov 13 '05 #1
16 6988
"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:bg**********@nic.grnet.gr...
I saw this code in C++ but when tried to C causes an error:
-------------
void function(int &a) { a = 5; }
-------------


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++
Nov 13 '05 #2
- Chameleon - wrote:
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.

--
Chris "electric hedgehog" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgrou...mp.lang.c.html
C welcome: http://www.angelfire.com/ms3/bchambl...me_to_clc.html
Nov 13 '05 #3
In <bg**********@nic.grnet.gr> "<- Chameleon ->" <ch******@hotmail.NOSPAM.com> writes:
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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #4
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 news:comp.std.c

--
Martin Ambuhl

Nov 13 '05 #5
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:2b*************@newsread2.news.atl.earthlink. net...
| No, this newsgroup is for standard C. The newsgroup for the C standard
| is news:comp.std.c
That's what i meant, give a guy some slack =)
Nov 13 '05 #6
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:q3*************@newsread2.news.atl.earthlink. net...
| 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.
Nov 13 '05 #7
> 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!
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?

Nov 13 '05 #8
> >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...


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


be cool
Nov 13 '05 #9
"Greg P." <no@spam.sam> wrote in message news:1w***************@newsread3.news.pas.earthlin k.net...
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:q3*************@newsread2.news.atl.earthlink. net...
| 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++.

--
#include <stdio.h>
char*f="#include <stdio.h>char*f=%c%s%c;main(){printf(f,34,f,34,10) ;}%c";
main(){printf(f,34,f,34,10);}
Nov 13 '05 #10
"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message news:<bg**********@nic.grnet.gr>...
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,
Nov 13 '05 #11
Greg P. wrote:
"Martin Ambuhl" <ma*****@earthlink.net> wrote in message
news:q3*************@newsread2.news.atl.earthlink. net...
| 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."


--
Martin Ambuhl

Nov 13 '05 #12
"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message news:<bg**********@nic.grnet.gr>...
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.

--
------------------- Richard Callwood III --------------------
~ U.S. Virgin Islands ~ USDA zone 11 ~ 18.3N, 64.9W ~
~ eastern Massachusetts ~ USDA zone 6 (1992-95) ~
--------------- http://cac.uvi.edu/staff/rc3/ ---------------
Nov 13 '05 #13
bd
On Thu, 31 Jul 2003 14:02:32 +0300, <- Chameleon -> wrote:
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++.

--
Freenet distribution not available
Chicken Little was right.

Nov 13 '05 #14


Martijn wrote:

<- Chameleon -> wrote:
Why this is not working in C?
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
Nov 13 '05 #15
> > ha ha! Ok! I thought that maybe it is standard C but I was wrong!
Thanks!


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!
Nov 13 '05 #16


"<- Chameleon ->" wrote:
ha ha! Ok! I thought that maybe it is standard C but I was wrong!
Thanks!


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

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
Nov 13 '05 #17

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

Similar topics

2
by: Jim Craig | last post by:
When I do a transform with a stylesheet containing the function format-number($varname,'#,##0.00') from XSLerator (which uses MSXML) I get the result I expect, a decimal number rounded to 2 places....
0
by: Phil C. | last post by:
Hi, I'm using Access 2000. I have a Select Query that uses the MID function to separate the actual text of articles from the title of the articles. The articles are enterd into the...
3
by: Ray Gardener | last post by:
I searched around but couldn't find any previous discussion on this, so... I have a macro that implements a common memory disposal function, e.g.: #define safe_free(void* pv) if(pv) {...
9
by: Alex Vinokur | last post by:
Is this approach safe? class Foo { // Stuff }; void func1 (void *) { // Do something
14
by: Bryan Parkoff | last post by:
Do you know that current C++ Compiler limits to 64KB segments in source code? It is good news that Microsoft Visual C++ 2005 has expanded to 4GB segments in source code. 4GB segment is ideal for...
5
by: jupiter | last post by:
hi friends, I have a program like some variable definations a function ( arg1, agr2): do something with arg1 & arg2 return a list
1
by: askcq | last post by:
what is the difference between these (void*)(&abc) and (void *)(abc) where abc is a local varaible inside a function
4
by: Oliver Block | last post by:
Hello, sometimes you can read that a function requiring an argument of tpye void ** is submitted something like the following: mystruct **ppval; myfunc((void **)&ppval, ...); I would...
3
by: monojohnny | last post by:
Hi, I know you can do stuff with introspection to gather up passed-in args for a Javascript function and that you can list all defined functions like: function a(abc,xyz,zzz) {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
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...

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.