分享

memcpy

 iDefine 2011-12-21
#include <stdio.h>
#include <string.h>
#include <conio.h>
typedef struct _STUDENT
{
int nNumber; // 学号
float fScore; // 分数
}STUDENT, *PSTUDENT;

STUDENT student[10]; // 定义20个大小的数组

int main(int argc, char* argv[])
{
int i;

// 数组初始化为0
memset(&student, 0, sizeof(student));

// 初始化数组前半部分,学号用数组的下标表示,分数用下标+50表示
for(i = 0; i < 10; i++)
{
student[i].nNumber = i;
student[i].fScore = (float)(i + 50.0);
}
// 显示数组的内容
for (i = 0; i < 10; i++)
{
printf("student[%d].nNumber = %d\nstudent[%d].fScore = %.1f\n\n",
i,
student[i].nNumber,
i,
student[i].fScore);
}
// 删除数组里面第2个元素,调整后续的存放空间。
memcpy(&student[1], &student[2], sizeof(STUDENT) * 8);
// 把最后的内容清零
memset(&student[9], 0, sizeof(STUDENT));
printf("删除后的内容:\n");
// 显示数组的内容
for (i = 0; i < 10; i++)
{
printf("student[%d].nNumber = %d\nstudent[%d].fScore = %.1f\n\n",
i,
student[i].nNumber,
i,
student[i].fScore);
}
getch();
return 0;
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多