Help with pointers

So for this program I need to find the mode of the array. but I have to use two functions and pass the array with pointers. I understand how the pointers work in theory they point the address using them however is a different story. I was just wondering what im doing wrong with pointers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;

void FILL_ARRAY(int *fillArray[]);

int main()
{
	const int SIZE = 20;
	int Array[SIZE] = { 0 };
	int *ptr = &Array[0];
	FILL_ARRAY(&Array);


	system("pause");
	return 0;

}

void FILL_ARRAY(int *fillArray[])
{
	for (int i = 0; i < 20; i++)
	{
		cin >> *fillArray[i];
		if (*fillArray[i] < 0 || *fillArray[i] > 1000)
		{
			cout << "enter a number 1-9 "; cin >> *fillArray[i];
		}
	}
}
Last edited on
code tags <> on the panel for editing posts help us a lot. Please use them.
1
2
3
4
5
6
7
8
9
10
11
void FILL_ARRAY(int *fillArray[]) //this is an array of pointers. 

void FILL_ARRAY(int *fillArray)  //this is just an array of ints.  
{
fillArray[i] = value; 
}

and in main
int x[1000];
fillarray(x); //this is all you need; c++ does some smoke and mirrors with array names and can treat the name of an array as if it were a pointer in most instances. 


you could make what you have work but you have an extra layer of pointers that you do not need (on top of the errors). Is the way I am showing you acceptable for your assignment?

Typically when using arrays you pass the size around. so a more realistic example (the above is pseudocode to get you there)::

1
2
3
4
5
6
void FILL_ARRAY(int *fillArray, const int size) 
{
    for(int i = 0; i < size; i++)
        fillArray[i] = i+1;
}

Last edited on
void FILL_ARRAY(int *fillArray[]);
You're declaring an array of pointers, not a pointer to an array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace std;

void FILL_ARRAY(int *fillArray);

int main()
{
    const int SIZE = 20;
    int Array[SIZE] = { 0 };
    FILL_ARRAY(Array);

    system("pause");
    return 0;
}

void FILL_ARRAY(int *fillArray)
{
    for (int i = 0; i < 20; i++)
    {
        cin >> fillArray[i];
        if (fillArray[i] < 0 || fillArray[i] > 1000)
        {
            cout << "enter a number 1-9 "; 
            cin >> fillArray[i];
        }
    }
}


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Edit: You should avoid the use of "magic numbers". You declare SIZE locally within main. If SIZE were to change, FILL_ARRAY would break. As jonin points out, you should pass it as an argument to that FILL_ARRAY will work with any size array.
Last edited on
Topic archived. No new replies allowed.