> On Mar 2, 2015, at 8:18 PM, Patrick J. Collins 
> <[email protected]> wrote:
> 
> Now, with this approach, I am not sure how to handle ints without having
> another method with duplicate code.

This is exactly one of the things C++ templates were designed to solve — you 
can make your function use a pseudonym “T” instead of “int” or “float”, and 
then operate on a vector<T>. However, C++ with templates is an awfully deep 
rabbit hole to jump into, especially if you’re not strong on C already.

The hacky C equivalent would be to put your function in a .h file, again using 
“T” instead of “int” or “float”. Then when you need the int version do:
        #define T int
        #include “myfn.h”
        #undef T
Etc. for float. Nasty but effective. But I’d recommend just using copy and 
paste instead.

> Also, I thought I'd also bring up my lack of familiarity with C gives me some
> confusion about how to pass arrays around.

In C an array is almost exactly the same as a pointer to its first element. 
(The difference is mostly in that when you declare an array as a variable, you 
get space allocated for its elements.)

>  float *floats = (float *)malloc(size * sizeof(float));
> and then I have to use &floats instead of floats?

Yes to the first line, no to the second. ‘floats’ is a pointer, so you can 
treat it as the array. floats[0] is the first element, etc.

Then to resize the array you’d do:
        floats = (float*) realloc(floats, newSize * sizeof(float));
and to free it, of course,
        free(floats);

—Jens
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Objc-language mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/objc-language/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to