0
votes

I got this warning "warning: assignment makes integer from pointer without a cast"! I want to figure out what does it mean? And what i need to change in my fucntion create_rectangle..... Thank you. Any help appreciated

struct point {
        int x;
        int y;
    };

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

and my code:

struct rectangle *create_rectangle(struct point ul, struct point lr,
                                   char *label) {

    struct rectangle *r = malloc(sizeof(struct rectangle));


    r->upperleft=ul;
    r->lowerright=lr;
    r->label[NAMESIZE+1]= strncpy(r->label,label,NAMESIZE);
   //here is the warning
      r->label[NAMESIZE] = '\0';

    return r;
}
3

3 Answers

0
votes

strncpy returns a pointer which you are assigning to a char. (Actually, you are assigning it to an illegal address as r->label[NAMESIZE+1] is beyond the bounds of the array.)

It should be

struct rectangle *create_rectangle(struct point ul, struct point lr,
                                   char *label) {

    struct rectangle *r = malloc(sizeof(struct rectangle));


    r->upperleft=ul;
    r->lowerright=lr;
    strncpy(r->label,label,NAMESIZE);
    r->label[NAMESIZE] = '\0';

    return r;
}
0
votes

The strncpy function returns a pointer to the destination string, and you are assigning this pointer to r->label[NAMESIZE+1]. This doesn't make much sense; if the intention is to copy label to the struct member you can just ignore the return value.

strncpy(r->label, label, NAMESIZE);
0
votes

The return value of strncpy is a pointer to r->label. You do not need to store it, especially not in unallocated memory.