Passing array to function

I

infobahn

DeepaK said:
Could anybody tell me how to pass array to a function by value?

Passing an array to a function is actually impossible. Passing by
value is easy enough, since C doesn't support any other form of
parameter passing.

Even /trying/ to pass an array by value smacks of silliness
(investigate const for when you pass the address of an object
that you don't wish the function to modify).

But I'm afraid there /is/ a way to do this damn silly thing.
Wrap the array in a struct, and pass the struct by value.

And don't tell anyone I told you.
 
O

osmium

DeepaK K C said:
Could anybody tell me how to pass array to a function by value?

Make a copy of the array in the calling function. Pass the name of this new
array to the called function.
 
T

Thomas Stegen

DeepaK said:
Could anybody tell me how to pass array to a function by value?

-Deepak

Three ways depending on how, why and where you want responsibilities
to be for this.

1. Put the array in a struct.
2. Create a copy of the array in the calling function.
3. Create a copy of the array in the called function.
 
L

Lawrence Kirby

As others have said taken literally this is not possible, C does not
support the passing of arrays to functions at all. But you can create an
equivalent effect.
Three ways depending on how, why and where you want responsibilities to
be for this.

1. Put the array in a struct.

If the array is already in a struct for other reason then this is fine.
But I've never come across a situation where this is a sensible thing to
do just for the purpose of passing the array "by value".
2. Create a copy of the array in the calling function.

Possible. In that case the function interface allows the array in question
to be modified (if it doesn't modify it there's little point in passing by
value). So maybe some calling functions care about this while others don't.
3. Create a copy of the array in the called function.

IMO this is the cleanest way. The corresponding parameter would be defined
as a pointer to const indicating to the caller that the array won't be
modified. memcpy() can be used to make the copy. Issues are determining
the size of the array (which is an issue anyway) and how to allocate space
for the copy.

Lawrence
 
E

E. Robert Tisdale

DeepaK said:
Could anybody tell me how to pass array to a function by value?
> cat main.c
#include <stdlib.h>
#include <stdio.h>

typedef struct doubleArray4 {
double x[4];
} doubleArray4;

int doubleArray4_print(FILE* fp, doubleArray4 A) {
int characters = 0;
for (size_t j = 0; j < 4; ++j)
characters += fprintf(fp, " %f", A.x[j]);
characters += fprintf(fp, "\n");
return characters;
}

int main(int argc, char* argv[]) {
double a[4];
for (size_t j = 0; j < 4; ++j)
a[j] = j;
fprintf(stdout, "a = ");

doubleArray4_print(stdout, *((doubleArray4*)a));

return EXIT_SUCCESS;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main
a = 0.000000 1.000000 2.000000 3.000000
 
S

SM Ryan

(e-mail address removed) (DeepaK K C) wrote:
# Could anybody tell me how to pass array to a function by value?

Wrap it inside a struct and pass the struct by value, or use Pascal.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top