# structures and functions

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

1. ### N YiannakouliasGuest

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

2. ### Nick AustinGuest

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

3. ### Artie GoldGuest

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
4. ### Morris DoveyGuest

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

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

You might find the info available through the link below to be
--
Morris Dovey
West Des Moines, Iowa USA

Morris Dovey, Sep 19, 2003
5. ### Martien VerbruggenGuest

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

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

Martien Verbruggen, Sep 19, 2003
6. ### Eric AmickGuest

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