Identifiers, Defining Variables and Pointers in C++

pointers

When following a tutorial, I made the value of x, 7 and the value of y, 42.

I then set the pointer value to assign the address of x, so for the first paragraph *ip=7
In the second paragraph x now has the value 73. So x = 73, y remains 42 and the *ip has changed in accordance to addressing x and is now 73.

In the third paragraph ip is assigned to the address of y.
So x = 73, y remains 42 and ip has changed in accordance to addressing y and is now 42.

This tutorial taught me about the following:

Identifiers:

Are tokens that provide readable names for variables, function, labels and defined types, made up of letters and numbers within a set of constraints.

Defining Variables:

Can be identified as:

int: i;
i = 7;

or as

int: i = 7;

variable i has a value of 7.

  • C++ uses qualifiers, sometimes called modifiers, that change the behaviour variables in various ways.
  • Eg. const qualifier = read only, prevents value being changed. Eg. const int = 7;
  • A variable is a typed and named location in memory
  • Memory is allocated for the size of an integer associated with the name x, integer value 1 is copied into the memory location associated with the integer variable x.

Pointers:

int: x;

x = 1;

int y = x;    is both a definition and an assignment

int *ip;   is a pointer to a value, ip is pointer to int

& = reference operator or address operator.

ip = &x  this assigns address of x to integer pointer ip

y = *ip  this copies the pointed value by ip which currently points to integer variable x which currently points to integer variable y. In this case the * is called the pointer dereference operator.

 

Advertisements