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 154 views
1 answer 215 views
3 answers 277 views
1 answer 165 views
1 answer 112 views
1 answer 152 views
...