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