Shorter Way to Pass Every Element of an Array to a Function

Shorter way to pass every element of an array to a function

You can do this

@files.map(&method(:read))

But be aware though about performance.

Pass all the values from an array into a function as parameters

Use the .apply method of the Function object.

window.myFunction.apply(window, ['a','b','c','d']);

The .apply method invokes the function you're calling, but lets you set the function's this value (the first argument) and lets you set its arguments using an Array (the second argument).

So here we kept window as the this value, and we're passing the Array as the individual arguments. The Array members will be distributed as though they were passed as individual arguments, so it's as if you had done this:

window.myFunction('a','b','c','d');

Pass in part of an array as function argument

You can manually increment the pointer by 1:

your_function(arr + 1)

Pointer arithmetic in C implicitly accounts for the size of the elements, so adding 1 will actually add 1 * sizeof(int)

For a closer analogue to array slicing from other languages, try this function:

int *slice_array(int *array, int start, int end) {
int numElements = (end - start + 1)
int numBytes = sizeof(int) * numElements;

int *slice = malloc(numBytes);
memcpy(slice, array + start, numBytes)

return slice;
}

It makes a slice of the array between the given start and end indices. Remember to free() the slice once you're done with it!

How can I pass an array to a function that cannot modify it?

First, the declarations const Foo array_in[] and Foo const array_in[] are exactly the same in C.

Second, when the parameter type of a function has "array of T" type, it is automatically adjusted to "pointer to T" type. So the compiler pretends as if you wrote:

void func(const Foo *array_in)

or equivalently

void func(Foo const *array_in)

Note that both const Foo *array_in and Foo const *array_in mean the exact same thing in C (pointer to const Foo). However, Foo * const array_in means something different (const pointer to Foo).

C++ Pass an array to a function and read its members

You cannot pass an array to a function in C++. There are several ways around this

1) Use vectors instead of arrays

2) Pass a reference to the array (this only works with a fixed size array)

3) Pass a pointer to the first element of the array (this requires that you pass the size as a seperate parameter).

Here's how you do all three

1) use vectors

#include <vector>

std::vector<unsigned char>{1,2,3,4,5,6,7,8}:
logger(numbers);

void logger(const vector<unsigned char>& data)
{
for (auto i = 0; i < data.size(); i++)
{
std::cout << (unsigned)data[i] << "\n";
}
}

2) use a reference

unsigned char numbers[8] = { 1,2,3,4,5,6,7,8 };
logger(numbers);

void logger(unsigned char (&data)[8])
{
for (auto i = 0; i < 8; i++)
{
std::cout << (unsigned)data[i] << "\n";
}
}

3) use a pointer

unsigned char numbers[8] = { 1,2,3,4,5,6,7,8 };
logger(numbers, 8);

void logger(unsigned char *data, size_t size)
{
for (auto i = 0; i < size; i++)
{
std::cout << (unsigned)data[i] << "\n";
}
}

vectors are the best solution. C++ has proper data structures as standard, use them.

As has already been explained your printing problems are due to the special rules for printing characters, just cast to unsigned before printing.

No code has been tested (or even compiled).

Passing arrays as arguments to a function without passing their length to that function

I read that while passing an array as argument we must also have to pass its length as argument

This applies only in situations when the function does not know the size of the array upfront. When the function knows that the array must have a certain number of elements, you do not need to pass the size to the function.

This is exactly what is happening in your code: both functions that receive arrays know that num_in_rank contains exactly NUM_RANKS elements, and num_in_suit contains exactly NUM_SUITS elements. That is why you do not need to pass the size, and nothing is going to happen: the function already knows the size through #defined constants.

On the other hand, if you needed to pass an array of size unknown to your function, then you would need to do one of the following:

  • Pass the number of elements in your array, or
  • Pass a pointer to the last element (or one past the last element) of your array, or
  • Agree on a "sentinel" value such as zero or negative one that would indicate the end of the valid range of data in your array.

Passing an array as a function parameter in JavaScript

const args = ['p0', 'p1', 'p2'];
call_me.apply(this, args);

See MDN docs for Function.prototype.apply().


If the environment supports ECMAScript 6, you can use a spread argument instead:

call_me(...args);

When passing an array to a function, is it necessary to provide the number of elements as an argument?

Is it necessary? Will my code compile if I don't pass array size as argument?

It is not necessary & your code will compile even if you do not pass array size as argument to the function.

But it is a good practice to do so.

Rationale:

C does not provide you means of bounds checking for arrays. You can access beyond the bounds of array through array indexing or pointer arithmetic(Under the hood,both are same) and the compiler does not need to warn you about it. But you will end up getting an Undefined Behavior.

So it is important that you only access memory which belongs to your array. To be able to do this you need to keep track of the size of the array.It is the user's responsibility to do so.

When you pass an array as argument it decays as pointer to first element of the array.

So sizeof(arg) inside the function will give you the size of the pointer not the array.

There is no way you can know the size of the array inside the function(exception of null terminated string, who's size can be obtained by strlen) unless you pass it as an argument or through some other bookeeping(like global variable etc).

Passing the size as an argument is the most obvious way to do this.



Related Topics



Leave a reply



Submit