存储器源代码简单C语言
时间: 2023-09-30 22:08:59 浏览: 41
以下是一个简单的存储器源代码的C语言实现:
```c
#include <stdio.h>
#define SIZE 10
int memory[SIZE]; // 存储器
// 存储器读操作
int read(int address) {
if (address < 0 || address >= SIZE) {
printf("Error: Invalid memory address!\n");
return -1;
}
return memory[address];
}
// 存储器写操作
void write(int address, int data) {
if (address < 0 || address >= SIZE) {
printf("Error: Invalid memory address!\n");
return;
}
memory[address] = data;
}
int main() {
// 写入数据
write(0, 100);
write(1, 200);
write(2, 300);
// 读取数据
printf("Data at address 0: %d\n", read(0));
printf("Data at address 1: %d\n", read(1));
printf("Data at address 2: %d\n", read(2));
return 0;
}
```
该代码定义了一个长度为10的存储器数组,并提供了读写操作函数。在主函数中,演示了如何通过存储器读写操作函数来读取和写入数据。