I have a top function with an array of pointers like this:
Membership *mf[5];
for(i=0;i<5;i++)
mf[i]=(Membership*) malloc(sizeof(Membership))
where Membership
is a structure. So far so good and when debugging, mf
shows all the 5 entities correctly. 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:
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. Please help me on how to change call_function()
(not the array of pointers).
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.
When you say that call_function
"can't have anything to do with pointers", it's unclear what you mean. 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 . This is not what you want, since call_function
takes an array rather than an array member . @caveman's comment is correct: you should pass the raw array (ie without dereferencing it).
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
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. 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.
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:
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.