structures and functions

Discussion in 'C Programming' started by N Yiannakoulias, Sep 19, 2003.

  1. Hi all,

    I'm playing around with structures and functions. This code
    is flawed, and I think I need help understanding the problem.
    Can anyone help?

    #include <stdlib.h>
    #include <stdio.h>

    struct coordinates
    {
    int x;
    int y;
    };
    struct coordinates *location(void)
    {
    struct coordinates *coord;
    coord = malloc(sizeof(coord)*2);
    /*should check for available resources...*/
    coord[0].x = 10;
    coord[0].y = 15;
    coord[1].x = 5;
    coord[1].y = 8;
    return coord;
    }
    int print_values(struct coordinates *location_s)
    {
    printf("%d,%d\n",location_s[0].x,location_s[0].y);
    printf("%d,%d\n",location_s[1].x,location_s[1].y);
    return 0;
    }
    int main(void)
    {
    struct coordinates *location_s;
    location_s=location();
    print_values(location_s);
    return 0;
    }

    N
     
    N Yiannakoulias, Sep 19, 2003
    #1
    1. Advertising

  2. N Yiannakoulias

    Nick Austin Guest

    On Fri, 19 Sep 2003 00:14:51 +0000 (UTC), N Yiannakoulias
    <> wrote:

    >Hi all,
    >
    >I'm playing around with structures and functions. This code
    >is flawed, and I think I need help understanding the problem.
    >Can anyone help?
    >
    >#include <stdlib.h>
    >#include <stdio.h>
    >
    >struct coordinates
    >{
    > int x;
    > int y;
    >};
    >struct coordinates *location(void)
    >{
    > struct coordinates *coord;
    > coord = malloc(sizeof(coord)*2);


    coord = malloc(sizeof(*coord)*2);

    > /*should check for available resources...*/
    > coord[0].x = 10;
    > coord[0].y = 15;
    > coord[1].x = 5;
    > coord[1].y = 8;
    > return coord;
    >}
    >int print_values(struct coordinates *location_s)
    >{
    > printf("%d,%d\n",location_s[0].x,location_s[0].y);
    > printf("%d,%d\n",location_s[1].x,location_s[1].y);
    > return 0;
    >}
    >int main(void)
    >{
    > struct coordinates *location_s;
    > location_s=location();
    > print_values(location_s);
    > return 0;
    >}


    Nick.
     
    Nick Austin, Sep 19, 2003
    #2
    1. Advertising

  3. N Yiannakoulias

    Artie Gold Guest

    N Yiannakoulias wrote:
    > Hi all,
    >
    > I'm playing around with structures and functions. This code
    > is flawed, and I think I need help understanding the problem.
    > Can anyone help?


    Well, it *would* help if you mentioned what flaw you were
    perceiving, but...

    >
    > #include <stdlib.h>
    > #include <stdio.h>
    >
    > struct coordinates
    > {
    > int x;
    > int y;
    > };
    > struct coordinates *location(void)
    > {
    > struct coordinates *coord;
    > coord = malloc(sizeof(coord)*2);


    ITYM
    coord = malloc(sizeof *coord * 2);

    Otherwise, you're just allocating enough space for two pointers to
    `struct coordinates'.

    > /*should check for available resources...*/
    > coord[0].x = 10;
    > coord[0].y = 15;
    > coord[1].x = 5;
    > coord[1].y = 8;
    > return coord;
    > }
    > int print_values(struct coordinates *location_s)
    > {
    > printf("%d,%d\n",location_s[0].x,location_s[0].y);
    > printf("%d,%d\n",location_s[1].x,location_s[1].y);
    > return 0;
    > }
    > int main(void)
    > {
    > struct coordinates *location_s;
    > location_s=location();
    > print_values(location_s);
    > return 0;
    > }
    >

    HTH,
    --ag

    --
    Artie Gold -- Austin, Texas
     
    Artie Gold, Sep 19, 2003
    #3
  4. N Yiannakoulias

    Morris Dovey Guest

    N Yiannakoulias wrote:

    > I'm playing around with structures and functions. This code
    > is flawed, and I think I need help understanding the problem.
    > Can anyone help?


    <Code snipped>

    N...

    I'm willing to respect your opinions. The answer to your question
    is "Probably."

    Would you be willing to compose a short statement of what you
    intended for the code to do and a description of what it's doing
    instead?

    You might find the info available through the link below to be
    helpful.
    --
    Morris Dovey
    West Des Moines, Iowa USA
    C links at http://www.iedu.com/c
     
    Morris Dovey, Sep 19, 2003
    #4
  5. On Fri, 19 Sep 2003 00:14:51 +0000 (UTC),
    N Yiannakoulias <> wrote:
    > Hi all,
    >
    > I'm playing around with structures and functions. This code
    > is flawed, and I think I need help understanding the problem.


    flawed in what way? What do you expect to happen and what happens
    instead?

    > #include <stdlib.h>
    > #include <stdio.h>
    >
    > struct coordinates
    > {
    > int x;
    > int y;
    > };
    > struct coordinates *location(void)
    > {
    > struct coordinates *coord;
    > coord = malloc(sizeof(coord)*2);


    coord = malloc(2 * sizeof *coord);

    You're allocating enough space for two pointers to struct coordinates,
    but you want enough space for two structs.

    > /*should check for available resources...*/


    indeed.

    > int main(void)
    > {
    > struct coordinates *location_s;
    > location_s=location();
    > print_values(location_s);


    You should probably also make sure to free() location_s again.

    Martien
    --
    |
    Martien Verbruggen | Freudian slip: when you say one thing but
    Trading Post Australia | mean your mother.
    |
     
    Martien Verbruggen, Sep 19, 2003
    #5
  6. N Yiannakoulias

    Eric Amick Guest

    On Fri, 19 Sep 2003 00:14:51 +0000 (UTC), N Yiannakoulias
    <> wrote:

    >I'm playing around with structures and functions. This code
    >is flawed, and I think I need help understanding the problem.
    >Can anyone help?
    >
    >#include <stdlib.h>
    >#include <stdio.h>
    >
    >struct coordinates
    >{
    > int x;
    > int y;
    >};
    >struct coordinates *location(void)
    >{
    > struct coordinates *coord;
    > coord = malloc(sizeof(coord)*2);


    You want to allocate structures, not pointers to structures, so the
    argument to sizeof should be *coord. The rest of the code looks OK.

    --
    Eric Amick
    Columbia, MD
     
    Eric Amick, Sep 19, 2003
    #6
    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. Xiangliang Meng
    Replies:
    1
    Views:
    1,663
    Victor Bazarov
    Jun 21, 2004
  2. kelvSYC

    Structures and Variable Functions

    kelvSYC, Sep 14, 2003, in forum: C Programming
    Replies:
    19
    Views:
    589
    Douglas A. Gwyn
    Sep 19, 2003
  3. tweak
    Replies:
    14
    Views:
    2,819
    Eric Sosman
    Jun 11, 2004
  4. Alfonso Morra
    Replies:
    11
    Views:
    754
    Emmanuel Delahaye
    Sep 24, 2005
  5. Sean Murphy
    Replies:
    0
    Views:
    95
    Sean Murphy
    Jan 7, 2014
Loading...

Share This Page