[英]Passing “array of pointers” to function
I have a top function with an array of pointers like this: 我有一个top函数,有一个像这样的指针数组:
Membership *mf[5];
for(i=0;i<5;i++)
mf[i]=(Membership*) malloc(sizeof(Membership))
where Membership
is a structure. 其中
Membership
是一种结构。 So far so good and when debugging, mf
shows all the 5 entities correctly. 到目前为止一直很好,在调试时,
mf
正确地显示了所有5个实体。 Problem is when I pass that array of pointers to a function (which can't have anything to do with pointers) like this: 问题是当我将指针数组传递给函数(它与指针无关)时,如下所示:
call_function(*mf)
where call_function()
is declared like this: 其中
call_function()
声明如下:
void call_function(Membership mf[5]){
normalize_function(mf[0],slope);
....
normalize_function(mf[5],fuel);
}
In there, mf
just becomes useless and with length 1 instead of 5. I can't understand what I'm doing wrong, even after searching/reading about this and debugging for a while. 在那里,
mf
只是变得无用,长度为1而不是5.我无法理解我做错了什么,即使在搜索/阅读这个并调试了一段时间之后。 Please help me on how to change call_function()
(not the array of pointers). 请帮我看看如何更改
call_function()
(不是指针数组)。
Edit: As suggested I did some changes to the code. 编辑:正如建议我对代码做了一些更改。
Edit2: In response to KyleStrand's answer: call_function()
can't have anything to do with pointers because that function is actually going to be implemented in FPGA (VHDL coding) and pointers there are unecessary complications. 编辑2:回应KyleStrand的回答:
call_function()
与指针无关,因为该函数实际上将在FPGA中实现(VHDL编码),指针存在不必要的复杂情况。
When you say that call_function
"can't have anything to do with pointers", it's unclear what you mean. 当你说
call_function
“与指针无关”时,你不清楚你的意思。 It appears that the function's last parameter is an array of Membership
s , and in fact an array is treated very much like a pointer in C. When you pass *mf
to the function, you are dereferencing the array, which is to say, you are accessing the first member of the array . 似乎函数的最后一个参数是一个
Membership
数组,实际上一个数组的处理方式与C中的指针非常相似。当你将*mf
传递给函数时,你正在取消引用数组,也就是说,你正在访问数组的第一个成员 。 This is not what you want, since call_function
takes an array rather than an array member . 这不是你想要的,因为
call_function
采用数组而不是数组成员 。 @caveman's comment is correct: you should pass the raw array (ie without dereferencing it). @ caveman的评论是正确的:你应该传递原始数组 (即没有解除引用它)。
You seem to have some type issues. 你似乎有一些类型问题。 This is the sort of format you should have.
这是您应该具有的那种格式。 It's a community wiki, so others can clean up and improve.
这是一个社区维基,所以其他人可以清理和改进。
void call_function (Membership* local_mf[5])
{
Membership* m = local_mf[0];
}
int main (void)
{
Membership* mf[5];
... // malloc, etc, etc.
call_function (mf);
}
Please clarify your question, it is unclear from your question what you would like to do. 请澄清您的问题,从您的问题中不清楚您想要做什么。
But 但
Membership *mf[5]
is a an array whose elements are pointers of type Membership 是一个数组,其元素是Membership类型的指针
if you pass mf
to a function, then you are passing an array as an argument to a function and mf
itself is treated as a pointer. 如果将
mf
传递给函数,则将数组作为参数传递给函数,并将mf
本身视为指针。 It points to the first element of its members. 它指向其成员的第一个要素。 So in this case
mf
becomes a pointer to the first Membership pointer in its elements. 所以在这种情况下,
mf
成为指向其元素中第一个Membership指针的指针。
so inside the function (outside the function as well): 所以在函数内部(函数外部):
*mf would give you a pointer of type Membership (the first pointer)
*(mf+1) would give you a pointer of type Membersip (the second element)
**mf would give you an element of type membership (the element which the first pointer evaluates to).
you cannot get the length of mf
or any array inside a function, because if you pass an array as an argument, it is treated like a pointer inside the function, so if you do something like: 你无法获得函数内部的
mf
或任何数组的长度,因为如果你将一个数组作为参数传递,它会被视为函数内部的指针,所以如果你做了类似的事情:
sizeof(array)/sizeof(first_element)
sizeof(array)
inside the function will always equal to the size of a pointer, which will be 4 or 8 bytes depending on your system. 函数内的
sizeof(array)
总是等于指针的大小,根据您的系统,它将是4或8个字节。
so if you are using sizeof(mf)/sizeof(Membership*)
to get the length of mf
inside your function, that just means 8/8
or 4/4
which will always equal 1. 因此,如果您使用
sizeof(mf)/sizeof(Membership*)
来获取函数内部的mf
长度,那就意味着8/8
或4/4
总是等于1。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.