假设我有以下结构和该结构的数组:
struct Fileinfo {
int ascii[128]; //space to store counts for each ASCII character.
int lnlen; //the longest line’s length
int lnno; //the longest line’s line number.
char* filename; //the file corresponding to the struct.
};
struct Analysis fileinfo_space[8]; //space for info about 8 files
我想有一个函数,将为这个数组添加一个新的结构 . 它必须使用void指针指向将结构存储为参数的位置
int addentry(void* storagespace){
*(struct Fileinfo *)res = ??? //cast the pointer to struct pointer and put an empty struct there
(struct Fileinfo *)res->lnlen = 1; //change the lnlen value of the struct to 1
}
我的问题是:
-
代替什么?我根据this Stackoverflow响应尝试了
(Fileinfo){NULL,0,0,NULL}
. 但我得到'错误:'Fileinfo'未声明(首次使用此功能) -
如何创建指向数组的void指针? (void *)fileinfo_space是否正确?
我需要使用 void *
作为此赋值的函数的参数 . 这不取决于我 .
1 回答
假设您有一些内存块作为
storagespace
void指针传递:你必须定义一个能够初始化的常量(除非你're using c++11), let'调用它
init
. 顺便说一句你的赋值是错误的:第一个成员是一个int数组 . 你不能将NULL
传递给它 . 只需将其填充为零,如下图所示 .然后将void指针转换为结构上的指针,然后通过复制init结构进行初始化,随意修改...