473,387 Members | 1,501 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.

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 1582
-----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

iD8DBQFEEJONagVFX4UWr64RAoDgAJ9dgor1Jw16Z8llSg6mU0 DEOJ/NvgCeKVgz
eAs16tYnqZ1MRQsPhvZB5ag=
=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**********************@j33g2000cwa.googlegroups .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_Keith) 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****@btopenworld.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_Keith) 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.org>):
"Vladimir S. Oka" <no****@btopenworld.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
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...
110
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...
9
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...
9
by: Cheney Stallman | last post by:
I have a class as following: class X { public: static X* Create() { return new X(); }
11
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
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...
3
by: deduction | last post by:
How do I assign one class to another? Such as ..class_a { .class_b }
41
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...
275
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
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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:
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...
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.