473,667 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

p[assing by reference

I am novice to C; or rather I have forgoteen C long ago cause I'm a
young programmer and i didn't do much of it.
I have int *a; int l;
I have some logic that creates an array of arbitrary length in a
function and I want it to stick it into a and its length into l.

I have the following function now:
int* ReadArray(int &l)
{
//do stuff
}

int *a;
int l;
a = ReadArray(l);

GCC 3.2 tells me I have "parse error before '&' token" on the line
where the function is declared.
I don't understand, my memory and examples on the internet tell me in
unison that it's the way to pass stuff by reference. Or at least to
declare it. What's wrong with it/how do I do it right?

Mar 9 '06 #1
7 1595
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

re******@gmail. com wrote:
I am novice to C; or rather I have forgoteen C long ago cause I'm a
young programmer and i didn't do much of it.
I have int *a; int l;
I have some logic that creates an array of arbitrary length in a
function and I want it to stick it into a and its length into l.

I have the following function now:
int* ReadArray(int &l)
{
//do stuff
}

int *a;
int l;
a = ReadArray(l);

GCC 3.2 tells me I have "parse error before '&' token" on the line
where the function is declared.
Yup. You do.
I don't understand, my memory and examples on the internet tell me in
unison that it's the way to pass stuff by reference.
C doesn't support passing data "by reference". The best you can get from
C is pass "by value".
Or at least to
declare it. What's wrong with it/how do I do it right?


You are writing C++ code, and trying to compile it as C code.

- --

Lew Pitcher, IT Specialist, Corporate Technology Solutions,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEEJONagV FX4UWr64RAoDgAJ 9dgor1Jw16Z8llS g6mU0DEOJ/NvgCeKVgz
eAs16tYnqZ1MRQs PhvZB5ag=
=edfR
-----END PGP SIGNATURE-----
Mar 9 '06 #2
Umm... how do I achieve that kind of thing then? I am also intending to
use function to read x-y sized matrix from a file, where x and y are in
that same file, I have to return its dimensions with it; I oculd stick
them intto int*, but is there a better way?

Mar 9 '06 #3
On Thursday 09 March 2006 20:27, re******@gmail. com opined (in
<11************ **********@j33g 2000cwa.googleg roups.com>):
I am novice to C; or rather I have forgoteen C long ago cause I'm a
young programmer and i didn't do much of it.
I have int *a; int l;
I have some logic that creates an array of arbitrary length in a
function and I want it to stick it into a and its length into l.

I have the following function now:
int* ReadArray(int &l)
{
//do stuff
}

int *a;
int l;
a = ReadArray(l);

GCC 3.2 tells me I have "parse error before '&' token" on the line
where the function is declared.
I don't understand, my memory and examples on the internet tell me in
unison that it's the way to pass stuff by reference. Or at least to
declare it. What's wrong with it/how do I do it right?


If you want to get answers in c.l.c, you should follow some simple
guidelines for posting and topicality. For the posting, read:
<http://cfaj.freeshell. org/google/>. For the topicality and etiquete
read: <http://clc-wiki.net/wiki/Introduction_to _comp.lang.c>.

One reason for me telling you this is that in your reply to Lew, you did
not quote what and who you were replying to. That is a Very Bad Thing.

Back to your problem...

Apart from what you were already told, you may achieve what you want by
re-phrasing your code like this:

int* ReadArray(int &l)
{
/* do stuff, but using *l instead of l */
}

int *a;
int l;
a = ReadArray(&l);

This simulates "pass by reference" at the cost of typing '*l' where
you'd want just 'l'. C always passes "by value", but you can pass a
pointer and change the original through it.

--
BR, Vladimir

H. L. Mencken suffers from the hallucination that he is H. L.
Mencken -- there is no cure for a disease of that magnitude.
-- Maxwell Bodenheim

Mar 9 '06 #4
int * ReadArray(int *l)
{
// do stuff
}
main()
{
int *a;
int l;
a = ReadArray(&l);
}
this is the normal way in c.
But if u want to return the dimentions along with array use a struct
defined as
struct a {
int row;
int column;
int *array;
};
create the array and struct dynamically using malloc
or calloc and return the address of the struct.

Mar 9 '06 #5
re******@gmail. com writes:
Umm... how do I achieve that kind of thing then? I am also intending to
use function to read x-y sized matrix from a file, where x and y are in
that same file, I have to return its dimensions with it; I oculd stick
them intto int*, but is there a better way?


Achieve what kind of thing?

Please read <http://cfaj.freeshell. org/google/> if you want anyone to
know what you're talking about.

C doesn't directly support pass-by-reference, only pass-by-value. You
can achieve the effect of pass-by-reference by passing a pointer (the
pointer is passed by value).

The unary "&" operator yields the address of its operand (i.e., a
pointer to it). The unary "*" operator dereferences a pointer (i.e.,
it yields the value of the object it points to).

--
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.
Mar 9 '06 #6
"Vladimir S. Oka" <no****@btopenw orld.com> writes:
[...]
Apart from what you were already told, you may achieve what you want by
re-phrasing your code like this:

int* ReadArray(int &l)
This should be "int *l", not "int &l".
{
/* do stuff, but using *l instead of l */
}


--
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.
Mar 9 '06 #7
On Thursday 09 March 2006 23:05, Keith Thompson opined (in
<ln************ @nuthaus.mib.or g>):
"Vladimir S. Oka" <no****@btopenw orld.com> writes:
[...]
Apart from what you were already told, you may achieve what you want
by re-phrasing your code like this:

int* ReadArray(int &l)


This should be "int *l", not "int &l".
{
/* do stuff, but using *l instead of l */
}


D'oh! (fx: slams head on desk) Careless copy'n'paste job.

--
BR, Vladimir

Support the American Kidney Foundation.
Don't wear your motorcycle helmet.

Mar 10 '06 #8

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

Similar topics

2
13116
by: RU | last post by:
Hi, I am working on a porting project to port C/C++ application from unixware C++, AT&T Standard components to g++ with STL on Linux. This application has been working properly on Unixware/C++/AT&T componets environment. I have been able to compile all modules after making necessary changes in LINUX/gcc/STL environment. We have two templates defined XList and XMap.
110
9900
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object must be an object instead of
9
2994
by: Sandy | last post by:
Hi, In one of my interview I was asked a question, whether using pointers for argument is efficient then reference or not. i.e. void fun(Complex *p) void fun(Complex &ref) can somebody tell me which one will be more efficient and why? as far as i know both must be same, because i read somewhere that internally
9
3177
by: Cheney Stallman | last post by:
I have a class as following: class X { public: static X* Create() { return new X(); }
11
2189
by: Doug | last post by:
Is there any harm in passing an object into a method with the 'ref' keyword if the object is already a reference variable? If not, is there any benefit?
13
17916
by: Abe Frohnman | last post by:
Hello all, I'm passing a reference to a class into the constructor of a form, like so: public MyForm(int count, ref Area myArea) {...} How can I use myArea outside the constructor? Should I create another Area and assign myArea to it (ie: Area foo = myArea;) or is there a better way? ~AF
3
4731
by: deduction | last post by:
How do I assign one class to another? Such as ..class_a { .class_b }
41
3640
by: Summercool | last post by:
Can we confirm the following? also someone said, Java also has "reference" like in C++, which is an "implicit pointer": Pointer and Reference --------------------- I am starting to see what pointer and reference are and how they relate to each other.
275
12248
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
8365
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
8883
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
8563
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
8646
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
7390
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...
0
4200
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...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2776
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
2
1778
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.