What is the best way to pass three dimensional arrays into functions in C?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
typedefis your friend.Edit
I don’t like the use of definite types as the argument to
sizeof.I added the way to get the sizes of the (sub-)arrays without directly specifying their types, but rather let the compiler infer the right type from the object definitions.
2nd Edit
As Per Eckman notes typedef-ing “bare” arrays can be dangerous. Note that in the code above, I’m not passing arrays themselves to the function
foo. I am passing a pointer to a “lower level” array.foo(), in the code above, accepts a pointer to an object of typedimension2. Thedimension3object is an array of elements ofdimension2type, not an object ofdimension3type (which isn’t even defined).But remember Per Eckman’s note.