By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,201 Members | 914 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,201 IT Pros & Developers. It's quick & easy.

Passing array to function

P: n/a
Could anybody tell me how to pass array to a function by value?

-Deepak
Nov 14 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
DeepaK K C wrote:

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.
Nov 14 '05 #2

P: n/a
"DeepaK K C" writes:
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.
Nov 14 '05 #3

P: n/a
DeepaK K C wrote:
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.

--
Thomas.
Nov 14 '05 #4

P: n/a
On Thu, 17 Feb 2005 14:56:21 +0000, Thomas Stegen wrote:
DeepaK K C wrote:
Could anybody tell me how to pass array to a function by value?

-Deepak

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
Nov 14 '05 #5

P: n/a
DeepaK K C wrote:
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
Nov 14 '05 #6

P: n/a
kc********@hotmail.com (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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
There are subtler ways of badgering a witness.
Nov 14 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.