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 6th, 2013, 04:32 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
Cannot get what is wrong????

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));
/* Just want to assign new rectangle's label, upperleft and lowerright members. Going to use strncpy to assign to the rectangle's label. But it gives a mistake to me;( */

r->ul = ul;
r->lr = lr;
r->label = label;
char strncpy = *label;

return r;
}

Reply With Quote
  #2  
Old February 6th, 2013, 05:01 PM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is online now
Contributing User
Dev Shed God 2nd Plane (6000 - 6499 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 6,252 dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level)dwise1_aol User rank is General 15th Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 5 Days 19 h 26 m 40 sec
Reputation Power: 1985
Use code tags to preserve your code's indentation (I also moved the open braces to a far better position):
Code:
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));
    /* Just want to assign  new rectangle's label, upperleft and lowerright members. Going to use  strncpy to assign to the rectangle's label. But it gives a mistake to me;( */ 

    r->ul = ul;
    r->lr = lr;
    r->label = label;
    char strncpy = *label;
    return r;
}

I highlighted the problem lines in red.

r->label = label; -- That does not at all do what you seem to think that it does. It does not copy a string from one char array to another; for that you need strcpy or strncpy. Rather, since label is a char pointer, it assigns one char pointer to another, so that in the end both pointers would be pointing to the exact same string. But the fatal error here is that r->label is not a char pointer, but rather a char array. You are trying to change the location of r->label, which is not allowed! Read the error message you got there again.

You want to copy a string from label to r->label? Then use a string-handling function like strcpy or strncpy.

char strncpy = *label; -- What the hell is that? Read the help file/man page on strncpy.

And also, when you get error messages or warnings and you want us to tell you what they mean, then at the very least show them to us! We're not mind-readers, I'll have you know!

Last edited by dwise1_aol : February 6th, 2013 at 05:03 PM.

Reply With Quote
  #3  
Old February 6th, 2013, 05:29 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
sorry, im a beginner in c. Anyway, thank you.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Cannot get what is wrong????

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