Thread: Strncpy mistake

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

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Strncpy mistake


    In the function CREATE RECTANGLE I need to assign to this function LABEL. I am using strcncpy copying from r to label...but I have mistake such

    struct.c:41: error: expected expression before ‘char’
    struct.c:41: error: too few arguments to function ‘__builtin_object_size’
    struct.c:41: error: expected expression before ‘char’
    struct.c:41: error: too few arguments to function ‘__builtin___strncpy_chk’
    struct.c:41: error: expected expression before ‘char’
    struct.c:41: error: too few arguments to function ‘__inline_strncpy_chk’

    Code:
    #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 upperleft, struct point lowerright, char *label) {
    struct rectangle *r = malloc(sizeof(struct rectangle));
    /* TASK 1: fill in the rest of this function */
    r->upperleft = upperleft;
    r->lowerright = lowerright;
    <b>strncpy(char *label, struct rectangle *r, 20);</b>
    return r;
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,251
    Rep Power
    2222
    The compiler already knows what datatypes the parameters of strncpy are, because that is given in the function prototype. That information does not belong in a function call.

    Calling a function in C is one of the first and most basic things that we learn. That we learn long before writing our own functions and working with structs. How is it that you hadn't learned it yet?


    Also, the reason why we stress the use of code tags so strongly is that it preserves your code's indentation, thus keeping it readable. Which implies that you need to indent your code.

    Why are you not indenting your code? It's more for your own benefit, you know.
    Last edited by dwise1_aol; February 7th, 2013 at 06:05 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    I try to practice without any help.....sorry, still beginner. So I need just remove types because they were declared before?
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    Code:
    strncpy(label,r, 20);
    I did change... I guess C doesnt like it
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,251
    Rep Power
    2222
    You remove the types because they do not belong in a function call. They do belong in a function header (the first line of the definition of a function) and in a prototype, but never in a function call.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    Originally Posted by dwise1_aol
    You remove the types because they do not belong in a function call. They do belong in a function header (the first line of the definition of a function) and in a prototype, but never in a function call.
    hmm I did change but still it is a mistake...
    Code:
    strncpy(label,r, 20);
  12. #7
  13. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,251
    Rep Power
    2222
    What did the warning tell you? Argument is of the wrong data type, right? Well, what does the documentation for strncpy tell you about its prototype? Read the man page; if you cannot find it on your system, then Google on man page strncpy.

    From http://linux.die.net/man/3/strncpy we have:
    char *strncpy(char *dest, const char *src, size_t n);

    Is r a char pointer (a char array would be equivalent to a char pointer)? No, it is not. So then it is not correct to try to pass it as a char pointer. Now, r is a struct that does have a char array as one of its fields, so r->label would be a valid argument.

    But stop and think of what you want to do. Do you really want to copy the garbage in r->label into label? Or did you pass label into the function so that its contents could be copied into r->label? Again, read the documentation for strncpy (AKA RTFM! for "Read The ... Manual!"). The first argument is the destination, where the string is to be copied to, while the second argument is the source, where the string is to be copied from. The second argument is even a const, which means that its value cannot be changed, so that's another clue.

    If my assumptions about your intent are correct, that function call should instead read:
    strncpy(r->label, label, 20);

    Read it, study it, understand it. And use what I just did to learn how to figure these problems out on your own, because all too often we find ourselves having to do just that. IOW, instead of just simply giving you a fish, I am trying to teach you how to fish.

IMN logo majestic logo threadwatch logo seochat tools logo