【发布时间】:2015-11-19 03:58:28
【问题描述】:
我已经初始化了 3 个使用 typedef 定义的缓存实例。我通过以下方式对它们进行了一些处理:
cache cache1;
cache cache2;
cache cache3;
int a;
void main(...) {
if (a == 0) {
cache1.attribute = 5;
}
else if (a == 1) {
cache2.attribute = 1;
}
else if (a == 2) {
cache3.attribute = 2 ;
}
但是现在我需要通过以下方式使设计模块化:
cache cache1;
cache cache2;
cache cache3;
void cache_operator( cache user_cache, int a ) {
user_cache.attribute = a;
}
void main(...) {
if (a == 0) {
cache_operator(cache1,5);
}
else if (a == 1) {
cache_operator(cache2,1);
}
...
我无法将缓存传递给该方法。我习惯了java编程,对c指针不是很熟悉。但是,如果我如上所示传递缓存本身,我将在堆栈上传递缓存的副本,然后产生与原始代码不同的结果。在将适当的缓存传递给函数并确保正确访问它时,如何正确地将第一个设计转换为第二个设计。
【问题讨论】:
标签: c pointers struct typedef modular