Passing arrays

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 

int *myArray[arraysize];                   //calling function in main
getInputs(&myArray[arraysize], arraysize);

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. :)
Last edited on
looks like you are using the * too much

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.

Last edited on
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.
Last edited on
your probably trying to access an illegal array element

remember array elements start at 0 not 1 so in your for loop x should be initalized to 0

for (int x = 0; x <= arraySize; x++)

and on the other end because array elements start at 0 you have to us 1 less than the array size. So arraySize - 1;

so try this for (int x = 0; x <= arraySize - 1; x++)
For starters
for (int x = 0; x <= arraySize - 1; x++)

what you really meant was
for (int x = -1; x <= arraySize; x++)

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]...

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.
Last edited on
i would be helpful if you could post your entire code so i can run it on my compiler.
Last edited on
Topic archived. No new replies allowed.