Hi i have an array

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

that i am trying to pass to a function

int determinePage(int virtual_addr){
  int page_num;
  int page_size = 4096;

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

however I am getting errors.Any help would be appreciated

You need to tell the compiler and the function that an array is being passed.

For a 1-D array:

returnType functionName(dataType [optionalDimension1Size]);  //general form of prototype
void someFunc(double []);  //example prototype
void someFunc(double [10]);  //example prototype

returnType functionName(dataType [optionalDimension1Size]) {/*...*/}  //general form of definition
void someFunc(double anArray[]) {/*...*/} //example definition
void someFunc(double anArray[10]) {/*...*/} //example definition

For a Multi-D array

returnType functionName(dataType [optionalDimension1Size][requiredDimensionXSize]);  //general form of prototype
void someFunc(double [][10]);  //example prototype
void someFunc(double [10][10]);  //example prototype

returnType functionName(dataType [optionalDimension1Size][requiredDimensionXSize]) {/*...*/}  //general form of definition
void someFunc(double anArray[][10]) {/*...*/} //example definition
void someFunc(double anArray[10][10]) {/*...*/} //example definition

Passing in element counts as additional arguments is a good idea.
For a 1-D array:

returnType functionName(dataType [optionalDimension1Size], int);  //general form of prototype
void someFunc(double [], int);  //example prototype
void someFunc(double [10], int);  //example prototype

returnType functionName(dataType [optionalDimension1Size], int elementCount) {/*...*/}  //general form of definition
void someFunc(double anArray[], int size) {/*...*/} //example definition
void someFunc(double anArray[10], int size) {/*...*/} //example definition

I don't know if i should post this because I'm not too good with C++ but ss far as I know you should to it like:

int determinePage(int virtual_addr[]){
....
}

So the compiler knows it is an array of unknown size.

That will work, but only for a 1-dimensional array or the first dimension of a multi-dimensional array. If you have a multi-dimensional array, values MUST be specified for dimensions 2-X

I have one dimensional array

I know. To prevent confusion later, I just wanted to make it clear that this:

void someFunc(double [][]);  //example prototype

Is NOT okay.

Personally, if you wanted to do that, I would pass a pointer to the array and the array's length.

void someFunc(double* myArray, long arrayLength)
{
     // Do stuff with array using arrayLength to ensure no out of bounds exception
     // Also note any changes made in here will be reflected in the original array.
}

You could use the

vector<int> variable;

statement and have more control over it. Even changing the size, not passing the length to the function through a variable and can use other userful functions contained in the vector class.

You could use the

vector<int> variable;

statement and have more control over it. Even changing the size, not passing the length to the function through a variable and can use other userful functions contained in the vector class.

A good idea, but based on the type of question, the OP should be warned about the additional overhead incurred by invoking the vector class.

Also, care should be especially taken when popping items off the vector, as the memory allocated for the previous size of the vector is not released (if you have 100 items and pop 50 off, the vector is still allocated enough memory for 100 items, even though it only contains 50)

Looking at the OP's question, the application could be memory critical. Passing pointers is the most memory efficient way of working with arrays.

@Ketsuekiame: Are lists allocated in a contigous way also (like vectors) or the elements are creted in various portions of memory (like pointers)?

This might be also a good way for him.

@Scu: To answer your question directly, it's pointer based. The way they work is actually very efficient and in 99% of cases I would recommend using vector/list.

There are a couple of downsides. Firstly, they don't tend to de-allocate memory once it has been allocated (like in my above post) this makes it quicker to add and remove items.

They are also inefficient at storing plain data (anything that isn't a class or complex struct).
(Storing an int = sizeof(int), invoking vector = sizeof(vector<int>) + (vectorcount * (sizeof(pointer) + sizeof(int)). Then you also have allocation, reallocation + error checking...)

I do use the word inefficient in the loosest context I can though. Comparatively speaking on modern machines the time difference is microseconds for large arrays. This is the trade-off for flexibility and functionality such as iterators.

If memory is not an issue and you're running on a fast machine there's no reason not to go with them for complex and large arrays. On a single dimensional array of 10 ints...There's no point choosing anything other than an array of int =)

If memory is not an issue and you're running on a fast machine there's no reason not to go with them for complex and large arrays. On a single dimensional array of 10 ints...There's no point choosing anything other than an array of int =)

And, if someone is having a problem of this magnitude with a lowly array, they certainly haven't learned vectors yet, so to use them would be a failure on their program.

Therefore, please refrain from recommending that which is clearly beyond their current level. Let the instructors teach, while we help, and at their level.

As you've quoted me I'm unsure if that post was directed at me or not. However, to prevent a misunderstanding if you were directing it at me, I wasn't trying to recommend the OP use vectors. I was actually saying he should be using simple arrays. The post itself was primarily directed at Scu and not the OP. As such, after that post, we continued our discussion in PM.

If it wasn't directed at me, then i apologise for making you read this text =)

It is directed at whoever reads the post. It was my addition to what you said (note the And). I was supporting you and taking it one step further.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.