473,378 Members | 1,688 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,378 software developers and data experts.

How to pass an array to a function?

I'm attempting to pass an array to a function and I'm getting "undefined reference to fillArray(int*)" when attempting to build. I've been searching online all evening to know avail.

This program is supposed to fill an array with randomly generated numbers, but I stripped it down to this code to find what was giving me an error. I'm using eclipse C++.

Here's a code snippet:

Expand|Select|Wrap|Line Numbers
  1. void fillArray(int);
  2.  
  3. int main ()
  4. {
  5.     int const size = 20;
  6.     int array1[size], array2[size], array3[size];
  7.     fillArray(array1);           
  8. }
  9.  
  10. void fillArray(int blankArray) {
  11.     cout << "Testing 1 2 3 Testing";
  12. }
  13.  
Thanks in advance, sorry if it's extremely obvious. :p
Jan 29 '11 #1
3 2177
horace1
1,510 Expert 1GB
you are passing an array into the function so the function header should be
Expand|Select|Wrap|Line Numbers
  1. void fillArray(int blankArray[]) {
Jan 29 '11 #2
alexis4
113 100+
Or you can declare a pointer to point to the element [0] of the array.

Expand|Select|Wrap|Line Numbers
  1. #define _SIZE  20
  2. void fillArray(int *ptr);
  3.  
  4. int main ()
  5. {
  6.     int array[_SIZE];
  7.     int *p;
  8.     p = &array[0];
  9.     fillArray(p);           
  10. }
  11.  
  12. void fillArray(int *ptr) {
  13.   for(int i = 0; i < _SIZE; i++)
  14.   {  
  15.     *(ptr + i) = i;  //array[i] = i
  16.   }
  17. }
Jan 29 '11 #3
weaknessforcats
9,208 Expert Mod 8TB
Note that when you pass an array to a function all you pass is the name of the array. In C/C++ the name of an array is the address of element 0. Therefore, your funciton argument is a pointer to element 0.

However, once ytou are inside your function you have no idea how many array elements there are. This is called decay of array. For this reason, you will need a second function argument for the number of elements.
Jan 29 '11 #4

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

Similar topics

5
by: John | last post by:
I would like to pass array variables between URLs but I don't know how. I know its possible with sessions, but sessions can become useless if cookies are disabled. I have unsuccessfully tried...
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...
5
by: wilson | last post by:
Dear all, In this time, I want to pass array to function. What should I declare the parameter in the function?i int array or int array? Which one is correct? ...
2
by: Ronnie Smith | last post by:
I am trying to pass a function (method) address to a user control to associate with an event. The small user control which sends events is contained in a larger user control group which is in...
4
by: _Mario.lat | last post by:
Hallo, I have a little question: In the function session_set_save_handler I can pass the name of function which deal with session. In Xoops code I see the use of this function like that: ...
6
by: Wijaya Edward | last post by:
Hi, How can I pass Array, Hash, and a plain variable in to a function at the same time. I come from Perl. Where as you probably know it is done like this: sub myfunc {
0
by: Jagdish | last post by:
Hello, Every body I have recently joined this group and I want to know how to pass Array of string to a Com object function.. Actually I want to pass String array from VB6 to Com Interface...
8
by: laredotornado | last post by:
Hi, I want to pass my function myFunc('a', 'b', 'c') as an argument to another function. However, this will not work doStuff('x', 'y', myFunc('a', 'b', 'c'))
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: 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
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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.