
January 2nd, 2013, 01:56 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
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.
|