我有以下代码:
#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);
}
NUM_STUDENT
S 可以从 1
修改为 20
。
但我收到以下警告:
Warning 420: Apparent access beyond array for function 'memcpy(void *, const void *, unsigned int)', argument 3 exceeds argument 2
问题是什么以及如何解决?
最佳答案
在Master
中,你只有
Student students[TIME_STUDENTS][NUM_STUDENTS];
并且 TIME_STUDENTS
小于 NUM_STUDENTS
,所以
memcpy(&info.student[0][0],
&master.student[0][0],
sizeof(Student) * NUM_STUDENTS * NUM_STUDENTS);
复制比源更多的字节。
关于c - 以下 memcpy 如何超出 C 中的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17112698/