C++ pointers
Learning pointers in C++ is simple and tomfoolery. With pointers, the execution of some C++ programming assignments can be improved, and a few errands, like unique memory allotment, can't be performed without pointers. Along these lines, to be a decent C++ developer, it is important to learn pointers.
As you most likely are aware, each factor has a memory area, and every memory area characterizes a location that can be gotten to utilizing the dash (and) administrator, which addresses a location in memory. Investigate the model beneath, which will yield the location of the variable characterized:
example
using namespace std;
int main()
{
// declare variables//
int var4 = 3;
int var5 = 24;
int var 6 = 17;
// print address of var4//
cout << "Address of var4: "<< &var4 << endl;
// print address of var5//
cout << "Address of var5: " << &var5<< endl;
// print address of var6//
cout << "Address of var6: " << &var6 << endl;
}
result
Address of var4: 0x7fff5fbff8ac
Address of var5: 0x7fff5fbff8a8
Address of var6: 0x7fff5fbff8a4
What is a pointer?
A pointer is a variable whose worth is the location of another variable, that is, the immediate location of a memory area. Very much like some other variable or consistent, you should proclaim a pointer prior to utilizing it to store the location of another variable. The overall type of a pointer variable revelation is:
type *var-name;
* var - name ;
Here, type is the base kind of the pointer, which should be a substantial C++ information type, and var-name is the name of the pointer variable. The reference bullet * used to proclaim a pointer is equivalent to the mark utilized in duplication. In any case, in this articulation, the reference mark is utilized to determine that a variable is a pointer. Coming up next are substantial pointer statements:
int *ip; /* a pointer to an integer */
double *dp; /* a double pointer */double * dp ; /* a pointer to a double */
float *fp; /* a pointer to a float */float * fp ; /* a pointer to a float */
char *ch; /* a char pointer */char * ch ; /* a char pointer */
using pointers in C++
While utilizing pointers, the accompanying tasks are oftentimes performed: characterize a pointer variable, allocate the variable location to the pointer, and access the worth of the location accessible in the pointer variable. These return the worth of the variable at the location indicated by the operand by utilizing the unary administrator * . The accompanying models include these activities:
example
using namespace std;
int main() {
int var = 5;
// declare pointer variable//
int* pointVar;
// store address of var//
pointVar = &var;
// print value of var//
cout << "var = " << var << endl;
// print address of var//
cout << "Address of var (&var) = " << &var << endl
<< endl;
// print pointer pointVar//
cout << "pointVar = " << pointVar << endl;
// print the content of the address pointVar points to//
cout << "Content of the address pointed to by pointVar (*pointVar) = " << *pointVar << endl;
return 0;
}
output
var = 5
Address of var (&var) = 0x61ff08
pointVar = 0x61ff08
Content of the address pointed to by pointVar (*pointVar) = 5
0 Comments
Post a Comment