Easy tutorial
for C / C ++ Pointer
Pointer :
- Let's look at pointers, one of the unique features of C
++ language. Commands and variables used in the program are stored in a
specific location in memory (RAM). At this time, the variables allocated to the
memory have their own addresses.
- Pointer represents the address of memory where data is
stored. It is a concept used to manage data in memory. First, the declaration
of the pointer variable is as follows:
Usage :
'Data type' * 'Pointer variable name';
- Similar to ordinary variable declaration except that
the name to be declared as a pointer variable is preceded by '*'.
ex) int * a;
- Declared an integer pointer variable named 'a'.
note)
- Here the pointer variable is a (not * a).
- Memory storage space named 'a' is allocated.
- The data type of the data stored at the address pointed
to by 'a' is an integer (int).
- Memory address is stored in 'a' (Constant value).
- To know the value stored at the address pointed to by
'a', precede 'a' with '*' (* a).
Address operator :
- The operator is used to find the address of the
variable being used.
Usage:
& 'Pointer variable name';
- Let's look more at pointers through a simple example.
Example Code
#include
<iostream>
using
namespace std;
int
main() {
int n ;
n = 2;
cout << " n = " << n
<< ", address of n = " << &n <<endl;
for(int i = 0; i < 5; i++){
cout << "Enter a natural number :
";
cin
>> n ;
cout << " n = " << n
<< ", address of n = " << &n <<endl;
}
return 0;
}
- Integer variable 'n' is declared. And we assign 2 to 'n'.
Then we will print out the value stored in 'n' and the address of variable 'n'.
- Let's use a for loop for a simple test. The "Enter
a natural number:" sentence is displayed and the keyboard is used to input
the value. The input value is assigned to the variable 'n'. Then, we will print
the value stored in 'n' and the address of variable 'n' again. This process
will be repeated five times.
results :
n
= 2, address of n = 0x7ffff887eac8
Enter
a natural number :
- The value stored in 'n' is output as the initial value
2, and the address value '0x7ffff887eac8' of the variable 'n' is output. Now,
let's try typing the other numbers.
- Enter the number 7 and hit enter to see that the
entered 7 is printed. You can see that the address value of the variable 'n' is
'0x7ffff887eac8' unchanged. If you enter a different number, you can see that
the address does not change and only the input number of n changes.
n
= 2, address of n = 0x7ffff887eac8
Enter
a natural number : 7
n
= 7, address of n = 0x7ffff887eac8
Enter
a natural number : 1
n
= 1, address of n = 0x7ffff887eac8
Enter
a natural number : 8
n
= 8, address of n = 0x7ffff887eac8
Enter
a natural number : 9
n
= 9, address of n = 0x7ffff887eac8
Enter
a natural number : 4
n
= 4, address of n = 0x7ffff887eac8
Example Code
#include
<iostream>
using
namespace std;
int
main() {
int
n ;
int *pn ;
n = 3 ;
pn = &n;
cout << " n =
" << n << ",
address of n = " << &n << endl;
cout << " *pn = " << *pn
<< ", pn = "
<< pn << endl;
return 0;
}
- We declared an integer variable 'n' and a pointer
variable 'pn'. '3' is assigned to 'n', and address value of variable 'n' is
assigned to 'pn'.
- In the first output statement, the variable 'n' is used
to output the value and address value assigned to 'n'. In the second output
statement, the pointer variable 'pn' is used to print the values stored in the
'pn' and locations (address) pointed to by 'pn'.
n = 3,
address of n = 0x7ffd3698dbd4
*pn
= 3, pn = 0x7ffd3698dbd4
- When you execute it, you can check that 'n' and '* pn'
are the same value.
No comments:
Post a Comment