### Thread: Cannot get what is wrong????

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
8
Rep Power
0

#### Cannot get what is wrong????

struct point {
int x;
int y;
};

struct rectangle {
struct point upperleft;
struct point lowerright;
char label[NAMESIZE + 1];
};

/* create_point dynamically allocates memory (using malloc) to store a point,
* and gives it initial values. Returns a pointer to the newly created structure
*/
struct point *create_point(int x, int y) {
struct point *p = malloc(sizeof(struct point));
if (p == NULL) {
perror("Error allocating space for point.");
exit(1);
}
p->x = x;
p->y = y;
return p;
}

/* create_rectangle dynamically allocates memory to store a rectangle,
* gives it initial values, and returns a pointer to the newly created rectangle.
*/
struct rectangle *create_rectangle(struct point ul, struct point lr, char *label) {
struct rectangle *r = malloc(sizeof(struct rectangle));
/* Just want to assign new rectangle's label, upperleft and lowerright members. Going to use strncpy to assign to the rectangle's label. But it gives a mistake to me;( */

r->ul = ul;
r->lr = lr;
r->label = label;
char strncpy = *label;

return r;
}
2. Use code tags to preserve your code's indentation (I also moved the open braces to a far better position):
Code:
struct point
{
int x;
int y;
};

struct rectangle
{
struct point upperleft;
struct point lowerright;
char label[NAMESIZE + 1];
};

/* create_point dynamically allocates memory (using malloc) to store a point,
* and gives it initial values.  Returns a pointer to the newly created structure
*/
struct point *create_point(int x, int y)
{
struct point *p = malloc(sizeof(struct point));
if (p == NULL)
{
perror("Error allocating space for point.");
exit(1);
}
p->x = x;
p->y = y;
return p;
}

/* create_rectangle dynamically allocates memory to store a rectangle,
* gives it initial values, and returns a pointer to the newly created rectangle.
*/
struct rectangle *create_rectangle(struct point ul, struct point lr, char *label)
{
struct rectangle *r = malloc(sizeof(struct rectangle));
/* Just want to assign  new rectangle's label, upperleft and lowerright members. Going to use  strncpy to assign to the rectangle's label. But it gives a mistake to me;( */

r->ul = ul;
r->lr = lr;
r->label = label;
char strncpy = *label;
return r;
}
I highlighted the problem lines in red.

r->label = label; -- That does not at all do what you seem to think that it does. It does not copy a string from one char array to another; for that you need strcpy or strncpy. Rather, since label is a char pointer, it assigns one char pointer to another, so that in the end both pointers would be pointing to the exact same string. But the fatal error here is that r->label is not a char pointer, but rather a char array. You are trying to change the location of r->label, which is not allowed! Read the error message you got there again.

You want to copy a string from label to r->label? Then use a string-handling function like strcpy or strncpy.

char strncpy = *label; -- What the hell is that? Read the help file/man page on strncpy.

And also, when you get error messages or warnings and you want us to tell you what they mean, then at the very least show them to us! We're not mind-readers, I'll have you know!
Last edited by dwise1_aol; February 6th, 2013 at 05:03 PM.
3. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Feb 2013
Posts
8
Rep Power
0
sorry, im a beginner in c. Anyway, thank you.