The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Strncpy mistake
Discuss Strncpy mistake in the C Programming forum on Dev Shed. Strncpy mistake C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 7th, 2013, 04:54 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 8
Time spent in forums: 1 h 11 m 13 sec
Reputation 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:
Original
- 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;
strncpy(char *label, struct rectangle *r, 20);
return r;
}
|

February 7th, 2013, 05:02 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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 05:05 PM.
|

February 7th, 2013, 05:07 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 8
Time spent in forums: 1 h 11 m 13 sec
Reputation Power: 0
|
|
|
I try to practice without any help.....sorry, still beginner. So I need just remove types because they were declared before?
|

February 7th, 2013, 05:11 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 8
Time spent in forums: 1 h 11 m 13 sec
Reputation Power: 0
|
|
Code:
strncpy(label,r, 20);
I did change... I guess C doesnt like it
|

February 7th, 2013, 05:13 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|

February 7th, 2013, 05:16 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 8
Time spent in forums: 1 h 11 m 13 sec
Reputation Power: 0
|
|
Quote: | 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);
|

February 7th, 2013, 05:43 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|