-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path9_pass_as_pointer.cpp
More file actions
49 lines (42 loc) · 1.11 KB
/
9_pass_as_pointer.cpp
File metadata and controls
49 lines (42 loc) · 1.11 KB
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
/*Goal: Learn to pass arrays to functions*/
#include<iostream>
#include<iomanip>
//Pass the array as a pointer
void arrayAsPointer(int *array, int size);
//Pass the array as a sized array
void arraySized(int array[3], int size);
//Pass the array as an unsized array
void arrayUnSized(int array[], int size);
int main()
{
const int size = 3;
int array[size] = {33,66,99};
//We are passing a pointer or reference to the array
//so we will not know the size of the array
//We have to pass the size to the function as well
arrayAsPointer(array, size);
arraySized(array, size);
arrayUnSized(array, size);
return 0;
}
void arrayAsPointer(int *array, int size)
{
std::cout<<std::setw(5);
for(int i=0; i<size; i++)
std::cout<<array[i]<<" ";
std::cout<<"\n";
}
void arraySized(int array[3], int size)
{
std::cout<<std::setw(5);
for(int i=0; i<size; i++)
std::cout<<array[i]<<" ";
std::cout<<"\n";
}
void arrayUnSized(int array[], int size)
{
std::cout<<std::setw(5);
for(int i=0; i<size; i++)
std::cout<<array[i]<<" ";
std::cout<<"\n";
}