1

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, mfjust 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.

16
  • 3
    Don't dereference mf when you call,i.e. call_function (..., mf) instead. Commented Feb 23, 2015 at 20:25
  • 2
    Shouldn't that declaration be void call_function(...,Membership[5]);, i.e., shouldn't it expect an array of type Membership (since mf is not a type)? Commented Feb 23, 2015 at 20:26
  • 2
    @JoãoPereira show the actual code, I sense someting fishy here. Commented Feb 23, 2015 at 20:27
  • 2
    Just wanted to say that arrays are syntactic sugar for pointers to memory address. So when OP is saying that function can have nothing to do with pointers and yet is passing an array to the function, this leads to a contradiction Commented Feb 23, 2015 at 20:42
  • 2
    A "pointer to the first element in a contiguous array of 5 Memberships" (a.k.a Membership mf[5]) is not directly convertible to "pointer to the first element in a contiguous array of 5 pointers, each of which point to a single Membership" (a.k.a Membership *mf[5]), or vice versa. Commented Feb 23, 2015 at 20:42

3 Answers 3

2

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 Memberships, 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 (i.e. without dereferencing it).

Sign up to request clarification or add additional context in comments.

Comments

1

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);
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.