structures and functions

N

N Yiannakoulias

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

Nick Austin

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

Artie Gold

N said:
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
 
M

Morris Dovey

N said:
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.
 
M

Martien Verbruggen

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
 
E

Eric Amick

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top