a simple newbie question in C

Discussion in 'C Programming' started by diffuser78@gmail.com, Feb 11, 2006.

  1. Guest

    I want to return an array from a function and catch it in an an array
    variable.

    For example like this

    int main(){
    int a[3];
    a = get_value();
    return 0;
    }

    int[] get_value(){
    int b[3]={0,1,2};
    return b;
    }


    This is a bit like Java. How to get this thing done in C.

    Please help
    , Feb 11, 2006
    #1
    1. Advertising

  2. Eric Sosman Guest

    wrote:
    > I want to return an array from a function and catch it in an an array
    > variable.
    >
    > For example like this
    >
    > int main(){
    > int a[3];
    > a = get_value();
    > return 0;
    > }
    >
    > int[] get_value(){
    > int b[3]={0,1,2};
    > return b;
    > }
    >
    >
    > This is a bit like Java. How to get this thing done in C.


    C is not Java, and one of the differences (there are
    many) is in the way arrays are implemented. It is sometimes
    said that arrays are not "first-class citizens" in C, mostly
    because there are only two operators that work on entire
    arrays: the `sizeof' operator and the `&' address-of operator.
    In particular, the `=' assignment operator doesn't work on
    arrays, so your attempt at `a = get_value()' is doomed from
    the start.

    What to do instead? Well, it depends on what you want
    to accomplish. Take a step back, realize that you're not
    writing Java, and don't let the syntactic similarity of C
    and Java sucker you into trying to use Java techniques in a
    C program. "Gift" is a perfectly good noun in both English
    and German (closely-related languages), but while you might
    give one to a friend you would not give it to a Freund.

    So: What's the larger problem you're trying to solve?

    --
    Eric Sosman
    lid
    Eric Sosman, Feb 11, 2006
    #2
    1. Advertising

  3. osmium Guest

    "Eric Sosman" writes:

    >> I want to return an array from a function and catch it in an an array
    >> variable.
    >>
    >> For example like this
    >>
    >> int main(){
    >> int a[3];
    >> a = get_value();
    >> return 0;
    >> }
    >>
    >> int[] get_value(){
    >> int b[3]={0,1,2};
    >> return b;
    >> }
    >>
    >>
    >> This is a bit like Java. How to get this thing done in C.

    >
    > C is not Java, and one of the differences (there are
    > many) is in the way arrays are implemented. It is sometimes
    > said that arrays are not "first-class citizens" in C, mostly
    > because there are only two operators that work on entire
    > arrays: the `sizeof' operator and the `&' address-of operator.
    > In particular, the `=' assignment operator doesn't work on
    > arrays, so your attempt at `a = get_value()' is doomed from
    > the start.
    >
    > What to do instead? Well, it depends on what you want
    > to accomplish. Take a step back, realize that you're not
    > writing Java, and don't let the syntactic similarity of C
    > and Java sucker you into trying to use Java techniques in a
    > C program. "Gift" is a perfectly good noun in both English
    > and German (closely-related languages), but while you might
    > give one to a friend you would not give it to a Freund.
    >
    > So: What's the larger problem you're trying to solve?


    I wasn't going to reply, but since I don't give a good God damn what the
    larger problem you are trying to solve is, herewith my reply.

    void fill(int b[])
    {
    for(int i=0; i<3; i++)
    b = i;
    }
    int main()
    {
    int a[3];
    fill(a);
    return 0;
    }
    ----
    There is nothing to return, a is modified in place.
    osmium, Feb 11, 2006
    #3
  4. writes:
    > I want to return an array from a function and catch it in an an array
    > variable.
    >
    > For example like this
    >
    > int main(){
    > int a[3];
    > a = get_value();
    > return 0;
    > }
    >
    > int[] get_value(){
    > int b[3]={0,1,2};
    > return b;
    > }
    >
    >
    > This is a bit like Java. How to get this thing done in C.


    Array types are second-class citizens in C. You can't assign them or
    return them from functions. You need to use pointers.

    If you have a C textbook, read the sections on arrays and pointers.
    If you don't, get one; I recommend Kernigan & Ritchie, _The C
    Programming Language_, Second Edition.

    See also section 6 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

    --
    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, Feb 11, 2006
    #4
  5. osmium wrote:
    [snip]
    > void fill(int b[])
    > {
    > for(int i=0; i<3; i++)
    > b = i;
    > }


    Or better:

    void fill(int b[], int len)
    {
    for (int i = 0; i < len; i++)
    b = i;
    }


    August

    --
    I am the "ILOVEGNU" signature virus. Just copy me to your
    signature. This email was infected under the terms of the GNU
    General Public License.
    August Karlstrom, Feb 13, 2006
    #5
  6. Guest

    hi, all!
    i think, you can write something, like that:

    int * get_array(int size){
    int i, *ptr = malloc(sizeof(int) * size); /* memory allocate*/
    for(i = 0; i < size; i++){
    ptr = i; /* fill array*/
    }
    return ptr; /* return pointer to array */
    }

    and from main it look like that:
    int i, size = 10, * arr = get_array(size);
    for(i = 0; i < size; i++){
    printf("%d\n", arr);
    }

    p.s sorry for my english
    ------------------------
    best regards
    Alexander Chugaevsky
    icq: 314285577
    , Feb 14, 2006
    #6
  7. wrote:
    > hi, all!
    > i think, you can write something, like that:
    >
    > int * get_array(int size){
    > int i, *ptr = malloc(sizeof(int) * size); /* memory allocate*/
    > for(i = 0; i < size; i++){
    > ptr = i; /* fill array*/
    > }
    > return ptr; /* return pointer to array */
    > }
    >
    > and from main it look like that:
    > int i, size = 10, * arr = get_array(size);
    > for(i = 0; i < size; i++){
    > printf("%d\n", arr);
    > }


    It is more flexible to let the client supply the buffer to be filled.

    > p.s sorry for my english


    No problem, but correct capitalization will certainly help.


    August

    --
    I am the "ILOVEGNU" signature virus. Just copy me to your
    signature. This email was infected under the terms of the GNU
    General Public License.
    August Karlstrom, Feb 14, 2006
    #7
  8. wrote:
    > hi, all!
    > i think, you can write something, like that:
    >
    > int * get_array(int size){
    > int i, *ptr = malloc(sizeof(int) * size); /* memory allocate*/
    > for(i = 0; i < size; i++){
    > ptr = i; /* fill array*/
    > }
    > return ptr; /* return pointer to array */
    > }
    >
    > and from main it look like that:
    > int i, size = 10, * arr = get_array(size);
    > for(i = 0; i < size; i++){
    > printf("%d\n", arr);
    > }
    >
    > p.s sorry for my english
    > ------------------------
    > best regards
    > Alexander Chugaevsky
    > icq: 314285577


    Hi
    Please don't forget to clean up by free()ing your malloc()ed array.
    Regards,
    Frodo Drogo Baggins
    Frodo Baggins, Feb 14, 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. Replies:
    0
    Views:
    519
  2. Kevin Spencer

    Re: Simple Simple question!!!

    Kevin Spencer, Jun 25, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    609
    Kevin Spencer
    Jun 25, 2004
  3. Daniel Frey

    Simple Question - Simple Answer?

    Daniel Frey, Dec 28, 2004, in forum: XML
    Replies:
    4
    Views:
    789
    Daniel Frey
    Jan 12, 2005
  4. Oli

    simple simple question

    Oli, Jan 26, 2004, in forum: ASP General
    Replies:
    10
    Views:
    316
    Roland Hall
    Jan 26, 2004
  5. Jerry C.
    Replies:
    8
    Views:
    202
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page