Pointers
Every variable is a memory location, which has its address defined.
That address can be accessed using the ampersand (&) operator (also called the address-of operator), which denotes an address in memory.
For example:
This outputs the memory address, which stores the variable score.
A pointer is a variable, with the address of another variable as its value.
In C++, pointers help make certain tasks easier to perform. Other tasks, such as dynamic memory allocation, cannot be performed without using pointers.
All pointers share the same data type - a long hexadecimal number that represents a memory address.
The only difference between pointers of different data types is the data type of the variable that the pointer points to.
A pointer is a variable, and like any other variable, it must be declared before you can work with it.
The asterisk sign is used to declare a pointer (the same asterisk that you use for multiplication), however, in this statement the asterisk is being used to designate a variable as a pointer.
Following are valid pointer declarations:int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch; // pointer to a character
Just like with variables, we give the pointers a name and define the type, to which the pointerpoints to.
The asterisk sign can be placed next to the data type, or the variable name, or in the middle.
Here, we assign the address of a variable to the pointer.
The code above declares a pointer to an integer called scorePtr, and assigns to it the memory location of the score variable using the ampersand (address-of) operator.
Now, scorePtr's value is the memory location of score.
Pointer Operations
There are two operators for pointers:
Address-of operator (&): returns the memory address of its operand.
Contents-of (or dereference) operator (*): returns the value of the variable located at the address specified by its operand.
For example:
The asterisk (*) is used in declaring a pointer for the simple purpose of indicating that it is a pointer (The asterisk is part of its type compound specifier). Don't confuse this with the dereference operator, which is used to obtain the value located at the specified address. They are simply two different things represented with the same sign.
Dereferencing
The dereference operator (*) is basically an alias for the variable the pointer points to.
For example:int x = 5;
int *p = &x;
x = x + 4;
x = *p + 4;
*p = *p + 4;
All three of the preceding statements are equivalent, and return the same result. We can access the variable by dereferencing the variable's pointer.
As p is pointing to the variable x, dereferencing the pointer (*p) is representing exactly the same as the variable x.
Source from : SoloLearn C++ Tutorial (https://www.sololearn.com)
'Programming > C++' 카테고리의 다른 글
프로그램 실행 시간 측정 방법 (clock()) (0) | 2020.12.28 |
---|---|
단축 평가 논리 계산법(Short-Circuit Evaluation) (0) | 2020.12.26 |
scanf() 입력 버퍼 비우는 방법 (0) | 2020.10.25 |
Dynamic Memory (동적 메모리) (0) | 2019.05.06 |
Naming Rules for Variables (변수 이름 생성 규칙) (0) | 2019.05.06 |
sort 함수 정렬 기준 (0) | 2018.11.17 |
랜덤 함수/난수 생성 함수 (Random Function) (0) | 2018.10.07 |
입력된 문자열에서 공백을 제거하여 출력하기 (0) | 2018.09.24 |