473,382 Members | 1,563 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

problem with passsing array to a function

Hi

I have an array defined as
double temp[5] = {0,0,0,0,0};

and a function
double function(double *something);

i am trying to do
double x = function(temp);

but this only passes the first element of the array and not the complete array to the function. How can I correct this?
Thank you.
Mar 12 '12 #1
3 1611
weaknessforcats
9,208 Expert Mod 8TB
The name of an array, temp in this case, is always the address of element 0. So temp is really a double* containing he address of
temp[0].

That means your function doesn't know how many elements are in the array unless you have a second argument with that information.

Next, you cannot return an array from a function. The rules on returns say you can return a type or a pointer to a type. An array doesn't fit this because you also need the number of elements.

You might read: http://bytes.com/topic/c/insights/77...rrays-revealed
Mar 12 '12 #2
The pointer in the function stores the base address,i.e,in this case temp[0].
To access the other elements you can use an offset i(say) and access the array as *(something+i).
Also you cannot return array from function and since you are passing the array by reference there is no need of doing this. after the main once the control passes to main() the array modifications done in the function will remain as the address of the array was passed.
Mar 15 '12 #3
weaknessforcats
9,208 Expert Mod 8TB
You cannot use *(something+i) because you don't know the size of the array. If you do this and access memory outside he array you will crash the program on a segmentation fault.

Also, arrays are not passed by reference. All that's passed is the address of element 0 and that is passed by value since the pointer something is not the same pointer as temp. There is no pass by reference in C.
Mar 15 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Hannu Tiitu | last post by:
Hi! I have a small problem with array and objects: $a = new A(); $b = new A(); $c = new A(); $d = new A(); $e = new A(); $ar = array($a, $c, $d);
1
by: Alexandru | last post by:
Hello fellows. i have a problem with unlink function. if i'm trying to use uplink like this: unlink("../home/meks_party.jpg"); i got the message: Warning: unlink(../home/meks_party.jpg): No...
2
by: BrianP | last post by:
Hi, I have had to invent a work-around to get past what looks like a JavaScript bug, the malfunctioning Perl-like JavaScript array functions including SPLICE() and UNSHIFT(). I have boiled it...
2
by: Michael Dekson | last post by:
In this VB function I have "Run-time error '3151' and debug is located at bold item (-->>). If you know what I do wrong please help me to solve this problem. Thanks
13
by: Ian Tuomi | last post by:
Could anyone please tell me what is wrong with this code? Compiler error is: "parse error before '.' token" in lines 13-16. I tried searching the internet but all the reference guides etc. say It's...
0
by: DWalker | last post by:
VBA, as used in Excel 2000 and Excel 2003, has a function called Array. It's commonly seen in statements like the following: Workbooks.OpenText Filename:=ACFileName, _ StartRow:=1,...
26
by: MLH | last post by:
How would I modify the following to achieve a 2-dimensional array? Dim MyWeek, MyDay MyWeek = Array("Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun") ' Return values assume lower bound set to 1...
5
by: Ranmini | last post by:
Dear prob solver, i have a problem with array lists. i am using array lists for adding objects. i just want to know, when you are adding an object to an arraylist whats the value u pass to the...
9
by: =?Utf-8?B?RGFya21hbg==?= | last post by:
Hi, I am wondering how you multi-dimension an array function? My declared function looks like this: Public Function GetCustomerList(ByVal Val1 As String, ByVal Val2 As Long, ByVal Val3 As...
1
by: john20 | last post by:
Hi All, I am having a problem with accesing function in the page. i have a page say user.aspx in the page load method i am calling function to validate the user. it works fine when single...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.