user3146
user3146

Reputation: 115

Is there a way to express an array in vectorized form in standard library in C?

I'm trying to enter the following array

float array[3] = {a, b, c};

into a function say

3DLocation(float x, float y, float z)

as

3DLocation(array[3])

instead of as

3DLocation(array[0], array[1], array[2])

so vectorized like R or Python (with certain libraries) or the like.
Is there a way to do this in the standard library(s)?
If not, is there a way to make a function to do this with only the standard library(s)?
If not, what is a library that can help me do this?
Also, is "vectorized" the term I'm looking for?
And, is "express" right, or should I maybe edit my question to say "access" instead?

Upvotes: 0

Views: 64

Answers (1)

John Bollinger
John Bollinger

Reputation: 181149

Is there a way to do this in the standard library(s)?

C has no built-in analog of Python's unary * operator (unary * has a different meaning in C, though analogous in some ways).

If not, is there a way to make a function to do this with only the standard library(s)?

You can write a helper function that does it:

void call_unpacked(void (*func)(float, float,float), float args[]) {
    func(args[0], args[1], args[2]);
}

Then you could use it as many times as you want:

call_unpacked(3DLocation, array);

You could also write call_unpacked() as a macro, which has a few advantages, or instead of a general-purpose wrapper you could write a function or macro that wraps 3DLocation() specifically:

void 3DLocation_unpacked(float a[]) {
    3DLocation(a[0], a[1], a[2]);
}

But there is no such function or macro in the standard library.

If not, what is a library that can help me do this?

Requests for library recommendations are off-topic here.

Also, is "vectorized" the term I'm looking for?

The usual terminology in Python (since you mention that language) involves forms of the word "unpack", such as I have used in the preceding. I don't think there's any cross-language consensus on terms for what you're talking about.

And, is "express" right, or should I maybe edit my question to say "access" instead?

"Express" doesn't seem right to me, though I understood what you meant. "Access" is worse. Personally, I would be inclined to stick with "unpack", rewording the question to make that fit.

Upvotes: 2

Related Questions