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 January 2nd, 2013, 01:17 PM
namso namso is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 namso User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 41 sec
Reputation Power: 0
Assigning a string pointer to an array string

I am attempting to assign a string pointer to a string array. The string array is declared within a struct (i.e. it is a struct member):

char ModuleGrade[5];

Here is the function which is returning the string pointer:

char *GetGrade(float OverallMark)
{
char *Grade;
Grade = malloc(80);

if (OverallMark < 40.00)
{
Grade = "Pass";
}

if (OverallMark >= 40.00)
{
Grade = "Fail";
}

return Grade;
}

The compile error states that that the "char *" and "char [5]" type strings are incompatible. It occurs on this line:

Student.ModuleGrade = GetGrade(Student.ModuleMark);

How can I go about about correcting this?

Thanks.

Reply With Quote
  #2  
Old January 2nd, 2013, 01:56 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,255 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 20 h 24 m 41 sec
Reputation Power: 1985
So what you want to do is to copy the string that the return value of GetGrade is pointing into the array Student.ModuleGrade? Is that correct?

Here is what you are trying to do with this line:
Student.ModuleGrade = GetGrade(Student.ModuleMark);
The compiler created two literal strings, "Pass" and "Fail" and stored them in two different memory locations, though probably next to each other (which doesn't matter). In the line, Grade = "Fail";, that assigned the address of the "Fail" literal string in the char* variable, Grade. When you return from GetGrade(), you return the address of the literal string that was selected within the function.

But then you try to assign that address to an array name, which is an enormous no-no! While you can use an array name as if it were a pointer in most cases, you can never try to change the array name's address. The array has a fixed location in memory; you cannot change it! In that line that throws the error, you are trying to change the address of Student.ModuleGrade, which you cannot do!

Instead, you need to copy the string that the return value of GetGrade is pointing to into the array, Student.ModuleGrade. You would use strcpy to do that. Be sure to #include the string.h header file.


BTW, if you had declared ModuleGrade to be a char pointer, then it would have worked. But you would probably not have understood exactly what was happening. In that case, the ModuleGrade pointer would just be pointing to the same literal string. That is not the same thing as having your own copy of the string.

Reply With Quote
  #3  
Old January 5th, 2013, 09:28 AM
namso namso is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 2 namso User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 30 m 41 sec
Reputation Power: 0
Strings

Thanks very much for the response. It helped get the end result I wanted.

My intention was to copy the string pointer into the string array rather than change its location!

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Assigning a string pointer to an array string

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