473,468 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Passing array to function

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

-Deepak
Nov 14 '05 #1
6 12644
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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
2
by: Steve Turner | last post by:
I have read several interesting posts on passing structures to C dlls, but none seem to cover the following case. The structure (as seen in C) is as follows: typedef struct tag_scanparm { short...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
20
by: jason | last post by:
Hello, I'm a beginning C programmer and I have a question regarding arrays and finding the number of entries present within an array. If I pass an array of structures to a function, then...
8
by: S. | last post by:
Hi all, Can someone please help me with this? I have the following struct: typedef struct { char *name; int age; } Student;
4
by: arnuld | last post by:
I am passing an array of struct to a function to print its value. First I am getting Segfaults and weired values. 2nd, is there any elegant way to do this ? /* Learning how to use an array...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.