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

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Dont know what is wrong ;(


    This is my code. In the function CREATE RECTANGLE I wanr just assign to new upperleft. but it gives a mistake "upperleft’ undeclared " ;(

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

    #define NAMESIZE 20

    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));
    /* TASK 1: fill in the rest of this function */
    Code:
     r->upperleft = upperleft;

    return r;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,174
    Rep Power
    2222
    upperleft is indeed not declared, just as you were told. The fix is to either declare it or to use the parameter that you did actually declare.

    Code:
    /* 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));
        /* TASK 1: fill in the rest of this function */
         r->upperleft = ul;  // instead of upperleft;
    
        return r;
    }
    And please use code tags.

IMN logo majestic logo threadwatch logo seochat tools logo