Pointer and declaration difficuty

Here's a piece of my code that I divided. I basically need to create 3 functions: 1)1st to fill my array with random numbers 2)2nd to print the array on the screen 3)3rd isnt included in this piece.

My problem is that I get the C2664 error:cannot convert parameter 1 from int[6][6] to int(*)[]. And I cant figure what's wrong with my code. Also I would like to check if how I wrote my pointers to fill and print the array is correct.

Thank you for your assistance

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 #include "stdafx.h"
#include <iostream>
#include <math.h>
#include <time.h>
#include<iomanip>
#include<array>
#include <algorithm>

using namespace std;
const int AS = 6;
void FillingRandomly(int *);
void printing(int *);


int c;

int main()

{
	int funny = 0;
	int timpa = 0;
	int counter = 0;
	int Array[AS][AS];
	srand(time(0));


	FillingRandomly(Array);	
	

	cout << "The unsorted array is" << endl << endl;

	printing(Array);


	cout << "The sorted array is" << endl << endl;



	

	
	
	system("PAUSE");


	return 0;

}

void FillingRandomly(int *Array)
{*Array=rand()%87 +12;
*Array++;
		}



void printing(int *ArrayPtr)
{
	int counter = 0;
	while(*ArrayPtr<AS*AS)
	{
	cout<<*ArrayPtr;
			*ArrayPtr++;
			if (*ArrayPtr%AS == 0)
				cout << endl << endl;
	}	
}
Your function prototypes should look like this
1
2
void FillingRandomly(int [][AS]);
void printing(int [][AS]);
Make life easier: define a type alias for the array int[6][6]
Pass the array by reference (to const).
To access its elements, use the simpler range based for loop.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// #include "stdafx.h"
#include <iostream>
#include <cmath> // <math.h>
#include <ctime> // <time.h>
#include <iomanip>
#include <array>
#include <algorithm>

//using namespace std;

const int AS = 6 ;

// if <array> was not included just for fun
// using array_type = std::array< std::array< int, AS > > ;

// otherwise
using array_type = int[AS][AS] ; // type of the array
// archaic syntax: typedef int array_type[AS][AS] ;

void FillingRandomly( /*int * */ array_type& ); // pass a reference to the array
void printing( /*int * */ const array_type& ); // pass reference to const array

//int c;

int main()
{
    // int funny = 0;
	// int timpa = 0;
	// int counter = 0;

	int Array[AS][AS];
	std::srand(time(0));
	FillingRandomly(Array);

	std::cout << "The unsorted array is\n\n" ; // << endl << endl;

	printing(Array);

	std::cout << "\n\nThe sorted array is\n\n" ;
}

void FillingRandomly( array_type& arr )
{
    for( auto& row : arr )
        for( int& v : row ) v = std::rand()%87 +12 ;
}

void printing( const array_type& arr )
{
    for( const auto& row : arr )
    {
        for( int v : row ) std::cout << std::setw(3) << v ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/9f09505172c20599
Isn't passing an array to a function automatically pass it by reference? So you don't need the & on the parameters because you can't pass array by value, you just have to tell the parameter that i will receive an array.

like this:
1
2
//function prototype
void getNum( int[][] );


or is this not allowed?
> you can't pass array by value,

True. Arrays are not copyable types.


> Isn't passing an array to a function automatically pass it by reference?

The only way to pass an array to a function is by reference. The reference syntax & has to be used.


> So you don't need the & on the parameters ...
> you just have to tell the parameter that i will receive an array.

If you omit the &, you are passing a pointer by value. The the actual argument is an array, an implicit array-to-pointer conversion is performed, and the resultant pointer is passed by value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void foo( int (&array)[10] ) {} // pass an array of type int[10] by reference

void bar( int array[] ) {}  // pass a pointer to int by value

void baz( int array[1000] ) {} // same as above; pass a pointer to int  by value

void foobar( int array[99999] ) {} // same as above; pass a pointer to int by value

void foobaz( int* array ) {} // same as above; pass a pointer to int by value

int main()
{
    int a[10] = {0} ;
    int b[20] = {0} ;

    foo(a) ; // fine; array of 10 int or int[10]
    // foo(b) ; // *** error: type mismatch (not array of 10 int )

    bar(a) ; // fine; array decays to a pointer, the pointer is passed by value
    bar(b) ; // fine; array decays to a pointer, the pointer is passed by value
    foobar(b) ; // fine; array decays to a pointer, the pointer is passed by value
    foobaz(b) ; // fine; array decays to a pointer, the pointer is passed by value
}
Last edited on
Topic archived. No new replies allowed.