So Im quite a novice. I have searched for some of this information but i dont know what im really doing wrong. TBH i am not 100% confident with arrays and pointers. I understand them but i cant seem to get the code down. Here is what i have.
int getInputs(int *[], int arrSize); //prototype
1 2
int *myArray[arraysize]; //calling function in main
getInputs(&myArray[arraysize], arraysize);
1 2 3 4 5 6 7 8 9
int getInputs(int *passArray[], int arrSize) //function
{
for (int x = 1; x <= arraySize; x++)
{
cout << "Please enter a number: ";
cin >> *passArray[x];
}
};
Before you call me out being obvious noob, i am. I have done some pointers and some arrays but putting them together is causing me some problems. I simply need to take an already initialized array size and ask the user for inputs based on that array size in the function and return it to the main. Again, any help is awesome, best practices should be left untill the end unless its necessary to fix the issue. :)
function prototype should look somthing like this
int function name (arraName[], int * pointerName);
although the pointer dosn't really need to be a pointer if anything it should be a constant referance, but thats another topic.
an array declareation should look like this
int arrayName[size];
if you do this int *arrayName[size]; you are creating an array of integer pointers thats not what you are going for.
function call should look like this
functionName (arrayName, &argumentName);
notice that the array dosn't have the [], and the pointer has a & (address of operator) at the beginning.
function header should look something like this
double functionName(int arrayName[], int *pointerName)
again the pointer dosn't really have to be a pointer here.
also note that there are two differant types of notation you can use when referanceing an array element.
arrayName[ i ], and *(arrayName + i) are the same thing.
I made the changes, and now it compiles but it will not take in the numbers. It crashes out when i start giving it numbers at the array in the loop in the getInputs function.
I am only using pointer for the array but passing it to the function then back is what i need to do.
but that doesnt work becasue i took the precausion to count AFTER the line is read not before. x++ is after, ++x is before. no need to do -1. In addition, if i read my array in 1-10 its a[1], a[2]...
1 2
int myArray[] = {{1,2,3,4}, {5,6,7,8}};
cout << myArray[0][4];
my output should be 8 iirc. My setup this is not the case. int x=1 should start me at myArray[1] as i have had the user enter their first number as x=1. So i am trying to pass that number to myArray from the function i have created using *passArray[1];
I think i have at least that part right, im just not getting the program to store then pass the info correctly because when i get to the part where it allows the user to input information, it crashes.