C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 7th, 2013, 04:54 PM
Beginner_in_C Beginner_in_C is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 8 Beginner_in_C User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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; }

Reply With Quote
  #2  
Old February 7th, 2013, 05:02 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,122 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 13 h 38 m
Reputation Power: 1949
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.

Reply With Quote
  #3  
Old February 7th, 2013, 05:07 PM
Beginner_in_C Beginner_in_C is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 8 Beginner_in_C User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #4  
Old February 7th, 2013, 05:11 PM
Beginner_in_C Beginner_in_C is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 8 Beginner_in_C User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #5  
Old February 7th, 2013, 05:13 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,122 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 13 h 38 m
Reputation Power: 1949
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.

Reply With Quote
  #6  
Old February 7th, 2013, 05:16 PM
Beginner_in_C Beginner_in_C is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 8 Beginner_in_C User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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);

Reply With Quote
  #7  
Old February 7th, 2013, 05:43 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,122 dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 14th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 3 Days 13 h 38 m
Reputation Power: 1949
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Strncpy mistake

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap