Hello all - I need homework help.

This part of the assignment is to create a struct, called StudentRecord, the first attribute of which needs to be size 20. My implementation of this is at (1).

After instantiating a StudentRecord named MyStudent, I'm to assign some value to it. I chose "Test Value" and my implementation is located at (2).

The problem is I continue getting this error:
error C2440: '=' : cannot convert from 'const char [20]' to 'char [20]
followed by:
There is no context in which this conversion is possible

It shows up right when I attempt to assign the char [20] a value.

#include "stdafx.h"
#include <iostream>
#include <string>

struct StudentRecord
{
	char Name[20];     // (1)
	int  ID;
	float GPA;
};

int main()
{
	StudentRecord MyStudent;
	MyStudent.Name = "Test Value";     // (2)  (3)
	MyStudent.ID	= 1234;
	MyStudent.GPA = 4.0;
	std::cin.get();
	return 0;
}

I've spent the last few days digging online for help - and have found no leads that make sense to me. I am assuming that I'm either asking the wrong questions, querying the wrong keywords, or don't fully understand the error.

Thus I ask you for help!

Please help me understand what the error message is trying to tell me.

Thank you for your time.

You can't assign to arrays like that. In this case you're pretty much stuck with copying the contents of the string literal to the array manually or with a function like strcpy that does it manually:

#include <iostream>
#include <cstring>

struct StudentRecord
{
	char Name[20];     // (1)
	int  ID;
	float GPA;
};

int main()
{
	StudentRecord MyStudent;
	strcpy ( MyStudent.Name, "Test Value" );     // (2)  (3)
	MyStudent.ID	= 1234;
	MyStudent.GPA = 4.0;
	std::cin.get();
	return 0;
}

Note that I changed the <string> header to <cstring>. <string contains the std::string object and helpers while <cstring> contains the string handling functions (including strcpy) and types inherited from C.

MyStudent.Name is the name of a storage location capable of holding up to 20 characters (including the terminating null character). You cannot simply assign a string constant to MyStudent.Name since that just tries to copy the address of the string constant to MyStudent.Name. What you need to do is copy the characters from the string constant into the space provided in MyStudent.Name. strcpy (MyStudent.Name, "Test Value"); (or strncpy for safety when using user input)

This question has already been answered. Start a new discussion instead.