use*_*316 1 c arrays struct memcpy
我有以下代码:
#define NUM_STUDENTS 20
#define TIME_STUDENTS 10
typedef struct
{
int name;
int age;
} Student;
typedef struct
{
int number;
int post;
} Contact;
typedef struct
{
int number;
Student students[TIME_STUDENTS][NUM_STUDENTS];
Contact contact[NUM_STUDENTS];
} Master;
typedef struct
{
int number;
Student students[NUM_STUDENTS][NUM_STUDENTS];
Contact contact[NUM_STUDENTS];
} Info;
Info info;
Master master;
//fill the data for master
if(NUM_STUDENTS < 10)
{
memcpy(&info.student[0][0],
&master.student[0][0],
sizeof(Student) * NUM_STUDENTS * NUM_STUDENTS);
}
Run Code Online (Sandbox Code Playgroud)
的NUM_STUDENTS可从被修改1到20.但是我得到了以下警告:
Warning 420: Apparent access beyond array for function 'memcpy(void *, const void *, unsigned int)', argument 3 exceeds argument 2
Run Code Online (Sandbox Code Playgroud)
问题是什么以及如何解决?
在Master,你只有
Student students[TIME_STUDENTS][NUM_STUDENTS];
Run Code Online (Sandbox Code Playgroud)
并且TIME_STUDENTS小于NUM_STUDENTS,所以
memcpy(&info.student[0][0],
&master.student[0][0],
sizeof(Student) * NUM_STUDENTS * NUM_STUDENTS);
Run Code Online (Sandbox Code Playgroud)
复制比源更多的字节.