#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep 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.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,175
    Rep Power
    2222
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    2
    Rep 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!

IMN logo majestic logo threadwatch logo seochat tools logo