c - 结构数组使奇怪的运行错误C
问题描述
我正在尝试运行此代码,并且在任何这些命令中都出现运行时错误:
strcpy( memories[i].name, "file");
memories[i].name[4]=i+48;
memories[i].name[5]='\0';
我试图理解这个原因很长一段时间,但找不到。特别是当此命令有效时:
strcpy( mm.name, "working file");
完整代码:
struct Memory {
char name[50]; //the name of the file
char storage[8000]; //the storage of the file
uint32_t address;
int sizeInUse; //bytes that are used
int memory_id;
};
//void initialMemory (struct Memory mm, struct Memory memories[]);
struct Memory mm; //working file
struct Memory memories[10];
//initialMemory ( mm ,memories);
strcpy( mm.name, "working file");
mm.address=&mm.storage;
mm.memory_id=-1;
mm.sizeInUse=0;
int i,j;
for(i=0; i<10; i++){
strcpy( memories[i].name, "file");
memories[i].name[4]=i+48;
memories[i].name[5]='\0';
memories[i].address= &memories[i].storage;
memories[i].memory_id=i;
int index=(i+1)*2; //we will fill (i+1)*200 places of memory i.
memories[i].sizeInUse= index;
for(j=0; j<index; j++){
memories[i].storage[j]=j+48;
}
memories[i].storage[index+1]='~';
}
解决方案
推荐阅读
- android - 创建存储库对象时出现实例化错误“java.lang.InstantiationError:”
- javascript - javascript对象的转换和删除一些字段
- javascript - ThreeJS 在物体靠近时将物体粘到另一个物体的边缘(如磁力拖动效果)
- java - JDBC顺序PreparedStatement?
- python - 这段代码是一个爱计算器的代码我怎样才能简化代码?
- javascript - 反应路由器使用参数返回空对象
- c# - 使用 C# openXml 在 Excel 文件中插入图像
- android - Android RegisterForActivity 不返回 Uri
- javascript - 停止计划的推送通知在反应本机推送通知中
- java - Eclipse Java 更新