pointers in c++ programming language

pointers in c++ programming language

Ordinarily we need not be concerned about where variables live in the computer’s memory during a program’s execution. The compiler generates machine code that takes care of those details for us.

Some systems software like operating systems and device drivers need to access specific memory locations in order to interoperate with hardware. Systems programmers, therefore, must be able to write code that can access such lower-level detail.

int *p;
int x;
x = 4;
Pointer declaration and assignment in c++
Pointer declaration and assignment in c++

The * symbol used as shown above during a variable declaration indicates that the variable is a pointer. It will be used to refer to another variable or some other place in memory. In this case, the sequence of assignments allows pointer p to refer to variable x.

int x;
x = 4;
int *p;
p = &x;
*p = 7;

copies the value 7 into the address referenced by the pointer illustrates the full sequence. Notice that the assignment to *p modifies variable x’s value. The pointer p provides another way to access the memory allocated for x.

*p = 5;

is the first assignment statement we have seen that uses more than just a single variable name on the left of the assignment operator. The statement is legal because the expression *p represents a memory location that can store a value

follow links