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

How to an array to a function

19
Hi i have an array

Expand|Select|Wrap|Line Numbers
  1. static int virtual_addr[10]=
  2. {8196,34893,38583,22883,61843,43532,333,8448,2334,9492};
that i am trying to pass to a function

Expand|Select|Wrap|Line Numbers
  1. int determinePage(int virtual_addr){
  2.   int page_num;
  3.   int page_size = 4096;
  4.  
  5.   page_num = floor(virtual_addr/page_size);
  6.    return page_num;
  7. }
however I am getting errors.Any help would be appreciated
May 14 '10 #1
19 1601
Markus
6,050 Expert 4TB
You're passing an array of integers to the function, not a single integer. Your function signature should reflect that: int determinePage(int virtual_addr[])
May 14 '10 #2
nlal
19
Thnaks for your response.
However i still get errors
May 14 '10 #3
Markus
6,050 Expert 4TB
And the error is?
May 14 '10 #4
nlal
19
too few arguments to function int determinPage(int*)
and
invalid operands int* and int to binary operator /
May 14 '10 #5
Markus
6,050 Expert 4TB
Can you show us the full code, please?
May 14 '10 #6
nlal
19
well that is the code.
I have an array of initialized variables and i m trying to pass its values to the function so that i can divide each value by 4096 and return each value to the main function.
May 14 '10 #7
Markus
6,050 Expert 4TB
That isn't the complete code. You don't show how you're calling the function, etc., nor the updated function.
May 14 '10 #8
nlal
19
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     cout << "Page Number is: " <<endl;
  4.     for (int i=0; i<10; i++){
  5.     cout <<determinePage()<< endl;
  6.     }
  7.  
  8. system("pause");
  9. return 0;
  10. }
however the error is generated in the function determinePage
May 14 '10 #9
Dheeraj Joshi
1,123 Expert 1GB
Do you have any function prototyping?
If yes. Please post it.

Regards
Dheeraj Joshi
May 14 '10 #10
nlal
19
No i don't have it
May 14 '10 #11
Dheeraj Joshi
1,123 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. cout <<determinePage()<< endl;
  2.  
You are calling a function without arguments.

and you say your function takes one argument.
Expand|Select|Wrap|Line Numbers
  1. int determinePage(int virtual_addr[])
  2.  
Regards
Dheeraj Joshi
May 14 '10 #12
Dheeraj Joshi
1,123 Expert 1GB
Are you trying to pass one value at a time to functions from your array?

Regards
Dheeraj Joshi
May 14 '10 #13
nlal
19
yes but my immediate concern is how to correctly pass the parameter to the function without getting errors
May 14 '10 #14
nlal
19
yes i am passing one value at a time
May 14 '10 #15
Dheeraj Joshi
1,123 Expert 1GB
Then array name and the parameter passed to the function can not be same.
It should be like.

Expand|Select|Wrap|Line Numbers
  1. int determinePage(int virtual_addr_i) // Note the name of the variable is not same as array name
  2. {
  3.    //Some stuff goes here
  4. }
  5.  
And function call must be like

Expand|Select|Wrap|Line Numbers
  1. for (int i=0; i<10; i++)
  2. {
  3.     cout <<determinePage(virtual_addr[i])<< endl;
  4. }
  5.  
Regards
Dheeraj Joshi
May 14 '10 #16
weaknessforcats
9,208 Expert Mod 8TB
The array name and the function argument name can certainly be the same since they are in sifferent scopes.

However, the array name is a pointer to element 0. Therefore, the function argument has to be an int* and not an int.
May 14 '10 #17
@nlal
int determinePage(int virtual_addr){
int page_num;
int page_size = 4096;

page_num = floor(virtual_addr/page_size);
return page_num;
}


your page_num = floor(virtual_addr/page_size)
is setting page_num = floor(array with 10 values divided by page_size);
you can't use an array like this, you have to pick a value from the array, or make a loop to use every value from the array.


Expand|Select|Wrap|Line Numbers
  1. int determinePage(int virtual_addr[]) //<-- the [] MUST be in there
  2. {
  3.   int page_num;
  4.   int page_size = 4096;
  5.  
  6.   page_num = floor(virtual_addr[0]/page_size); // must choose one value from the array to use or it has ten values and can't use them all like this.
  7.    return page_num; 
  8. }
  9.  
May 14 '10 #18
the function named floor(.......) used is not identified by c++ complier or is it user defined function?
May 14 '10 #19
IF you dont use the function floor(...) in your code then I made simple solution may it will help you. Thanks
#include<iostream>
using namespace std;
int determinePage(int);


int main()
{
int page_number;
static int virtual_addr[10]=
{8196,34893,38583,22883,61843,43532,333,8448,2334, 9492};

cout << "Page Number is: " <<endl;
for (int i=0; i<10; i++)
{
page_number = determinePage(virtual_addr[i]);
cout <<page_number<< endl;
}

system("pause");
return 0;
}

int determinePage(int virtual_addr)
{
int page_num;
int page_size = 4096;
page_num = virtual_addr/page_size;
return page_num;
}



out put:
Page Number is:
2
8
9
5
15
10
0
2
0
2
Press any key to continue . . .
May 14 '10 #20

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

Similar topics

5
by: Raju V.K | last post by:
I am developing a web site which requires a set og unique values be transferred between pages and server many times. There fore i decided to put the values in an array so that only one reference to...
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: Oeleboele | last post by:
OK, I can't understand this exactly: I have this function: int howdy(void *) I first past this to the function &aCharArray I realized later that this should be aCharArray but... the former...
6
by: DeepaK K C | last post by:
Could anybody tell me how to pass array to a function by value? -Deepak
2
by: Manish | last post by:
Hi, I m facing a problem in re-writing a code from VB to C #. We are using a third party DLL to develop a application of Fax. The current application is already written in VB and we have to...
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...
18
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to...
3
by: shobu | last post by:
passing array checkbox value and update the database <?include 'dbconnect.php'; error_reporting(0);$update_qr="update...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.