p[assing by reference

Discussion in 'C Programming' started by realgeek@gmail.com, Mar 9, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    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

    iD8DBQFEEJONagVFX4UWr64RAoDgAJ9dgor1Jw16Z8llSg6mU0DEOJ/NvgCeKVgz
    eAs16tYnqZ1MRQsPhvZB5ag=
    =edfR
    -----END PGP SIGNATURE-----
    Lew Pitcher, Mar 9, 2006
    #2
    1. Advertising

  3. Guest

    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, 2006
    #3
  4. On Thursday 09 March 2006 20:27, opined (in
    <>):

    > 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
    Vladimir S. Oka, Mar 9, 2006
    #4
  5. Guest

    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, 2006
    #5
  6. 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) <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.
    Keith Thompson, Mar 9, 2006
    #6
  7. "Vladimir S. Oka" <> 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) <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.
    Keith Thompson, Mar 9, 2006
    #7
  8. On Thursday 09 March 2006 23:05, Keith Thompson opined (in
    <>):

    > "Vladimir S. Oka" <> 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.
    Vladimir S. Oka, Mar 10, 2006
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Darren
    Replies:
    0
    Views:
    481
    Darren
    Oct 11, 2004
  2. Dude
    Replies:
    0
    Views:
    337
  3. ce
    Replies:
    1
    Views:
    2,960
  4. Mikko Ohtamaa
    Replies:
    2
    Views:
    402
    John J. Lee
    Jul 4, 2003
  5. Cheney Stallman

    How to assing this to NULL?

    Cheney Stallman, Oct 16, 2005, in forum: C++
    Replies:
    9
    Views:
    322
    Greg Comeau
    Oct 18, 2005
Loading...

Share This Page