I tried to make a simple function that takes an array parameter and outputs it and it keeps giving me th error:
\function test.cpp(12) : error C2664: 'func' : cannot convert parameter 1 from 'int' to 'int []'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Here is my program:

#include<iostream>
#include<string>
int func(int arr[])
{
return (arr[1]);
}
int arr[];
using namespace std;
int main()
{

cout<<func(1);
system("pause");
return 0;
}

I am really confused now to what it means when you have an array parameter like int func(arr[]) -does it define its length or the first parameters, how do you call it then?! HELP ME!
thank you.

line 8 belongs up between lines 2 and 3. The using statement should appear immediately following all includes and before any functions.

line 7: you can't declare arrays without the dimensions.

line 12: you have to pass an array. You call it like this:

int arr[10]; // an array with 10 elements

fund( arr );  // pass array to the function

Okay my program is now:

#include<iostream>
#include<string>
using namespace std;
int func(int arr[])
{
return (arr[5]);
}
int arr[3];

int main()
{

cout<<func(arr);
system("pause");
return 0;
}

It works BUT it just returns 0 and i dont have a clue why. ALso, when calling the function how would you declare it and what would it do -set the length ??

You are passing arr, an array of length 3 to the function, and returning arr[5].

arr[5] does not exist, as far as your program is concerned. The array does not get resized. In fact, by doing this (accessing the value of something that does not exist) your program is exhibiting undefined behaviour.

The term "undefined" in the C and C++ standards has (ironically enough) a specific meaning. It means that, as far as the standard is concerned, the results from running your program can be anything. The function might print a value of zero. It could also (conceivably) reformat your hard disk. Given that these and many other possibilities are allowed, the message is: don't write or run code like this.

If you change your line in main() to;

cout<<func(arr[10]);

the compiler will generally complain. arr is an array of integer. arr[10] is the value of the 11th element of that array (which doesn't exist either, so accessing it also causes undefined behaviour - refer above). Either way, an element of an array and the array are different things: in C++, they show up as having different types, and passing something of type A to a function that expects an argument of a completely different type B is not allowed.

when do you mean buy "why won't it work ...". Do you take your car to the auto repair shop and tell them "its broke -- pleas fix it." ? We need a lot more information.

In the code you posted, line 6 is returning the 6th element of the array, but line 8 only allocates an array with 3 elements.

Comments
it was true and helpful

When you declare an integer array of say size 3, you are actually statically allocating storage in memory for 3 integers, starting from the address the array/pointer points to.

When you try to access say the 5th element of the above said array, you are actually trying to access a memory location that you did not allocate for. Therefore, this could lead to unpredictable behaviour, and even crashing of the program because it is trying to access an invalid memory location which is probably hold by other process/application.

Thank you for your help but that was not really what my intention was. I was experimenting with the program to find any clue to what my original question was when I made it return an invalid array memory adress. My original question was: when using the function func(arr[]) what would the caller look like and what would its effect be?(would it define arr length, make santa kill a kitten,...)

Function call should be like this

func(array_name); //where array_name is the name of your array.

You should pass the array without any index, because passing with an index makes the value at that index to be passed as argument not the array. And you second question's answer -
Why will the function call define the array length? Array has to be defined before its passed, either dynamically or static by you.

This article has been dead for over six months. Start a new discussion instead.