Easy tutorial for C / C ++ - Arrays 3 (Passing an array as an argument)
- When you use arguments to a function, you need to
create as many arguments as needed. So what if you have a large number of
arguments? It is easier to write a program by passing the name of the array as
an argument to the function.
- We have reviewed earlier that the name of the array is
the address of the first element in the array. Let's take a look at the use of
array names as arguments to functions. Let's look at an example.
Example Code
#include
<iostream>
using
namespace std;
void
check(int*);
int
main() {
int arr[100] ;
for(int i = 0; i < 100; i++){
arr[i] = i ;
}
check(arr);
return 0;
}
void
check(int *p){
cout<< "arr[5] =
"<<p[5]<< endl;
cout<< "arr[30] =
"<<p[30]<< endl;
}
- Declare a function 'check' to check the element value
of the passed array. We declared it as 'int *' because the argument to be
passed is a pointer variable.
- Declared array 'arr' with 100 elements in main
function. We then assigned values to each element through a for loop. The first
value is 0 (arr [0] = 0) and the last value is 99 (arr [99] = 9).
- The 'check' function with argument which is a pointer
variable is called. I used the array name 'arr' that I defined before as an
argument. This is because the name of the array is a pointer variable that
points to the address of the first element in the array.
- In the definition part of 'check', you can see the
argument called int * p. Here, the name p is a pointer variable that I set
arbitrarily. This 'p' is used within the 'check' function and represents the
array 'arr' of the main function. The 'p' passed in can be used like an array.
To test, we will print p [5] and p [30].
results
:
arr[5]
= 5
arr[30]
= 30
- Notice that the value of the element stored in the
array 'arr' is output. (arr [5] = p [5], arr [30] = p [30]) As a result, we
have written a program that passes the array name as a parameter.
Pointer operation :
- You can apply arithmetic operators to pointers to
access other values.
- If you put a '*' in front of a pointer variable, it
means that it is the value stored in the address pointed to by the address value.
- Using the '+' operator shows the value stored in the
place indicated by next address value. '-' indicates the value stored in the
place indicated by the previous address value.
- We will revise the 'check' function definition in the previous example.
void
check(int *p){
cout<< "*p = "<<*p << endl;
cout<< "p[0] = "<<p[0] << endl;
cout<< endl;
cout<< "*(p+2) =
"<<*(p+2) << endl;
cout<< "p[2] = "<<p[2] << endl;
}
*p
: You can get the first element value of an array by
prepending '*' to the pointer variable 'p'. To see this, let's compare the
output of 'p [0]'.
*(p+2)
: From the first element of the array, you can get the
value that the address of the element the next to the next is pointing to.
Let's compare 'p [2]'.
results
:
*p = 0
p[0] = 0
*(p+2)
= 2
p[2] = 2
- You can
see that the result of using the index of the array and the result of
performing the pointer operation are the same.
No comments:
Post a Comment