How to pass int array to function as a pointer in C++

2 Answers

0 votes
#include <iostream>

using std::cout;
using std::endl;

void print(const int* const arr, const int TOTAL) {
	for (int i = 0; i < TOTAL; i++)
		cout << arr[i] << endl;
}

int main()
{
	const int TOTAL = 3;
	int arr[TOTAL] = { 123, 21, 99 };

	print(arr, TOTAL);
	
	return 0;
}

/*
run:

123
21
99

*/

 



answered May 2, 2018 by avibootz
0 votes
#include <iostream>

using std::cout;
using std::endl;

void print(const int* const p, const int TOTAL) {
	for (int i = 0; i < TOTAL; i++)
		cout << *(p + i) << endl;
}

int main()
{
	const int TOTAL = 3;
	int arr[TOTAL] = { 123, 21, 99 };

	print(arr, TOTAL);
	
	return 0;
}

/*
run:

123
21
99

*/

 



answered May 2, 2018 by avibootz

Related questions

2 answers 163 views
1 answer 224 views
3 answers 281 views
1 answer 172 views
1 answer 104 views
1 answer 116 views
1 answer 156 views
...