Background info:
what (exactly) is a
data type
- Data type:
- A data type
defines
a
domain of
values used
to
represent
a certain
category of
information
|
- A data type
in a programming language
provides
the
following
capabilities:
- Allow the
definition (= creation)
of
variables
of the data type
to
store values (= information)
of that data type
-
Provide
specialized
operations
on
variables/values of the
data type
|
- Example: the
data type
int
(represents
whole numbers)
(1) Definition of int typed variables: int x, y, z;
(2) Operations on int values: +, - , *, /, %
|
|
Prelude to
the reference
data type - review: what is a
reference
Reference data type
-
Reference
data type:
- A reference
data type =
a data type that
uses
integer numbers as
memory addresses
- I.e.:
- A reference
value is an
integer
- The program will
use
the integer as
an
memory addresses
|
|
-
Furthermore:
C allows
programmers to:
-
Define
variables of a
reference type that
can
store
memory addresses
I.e.: a variable that
stores an
integer that
is
used
as a memory addresses
- Apply
special operations
(& and
*) on these
integers (= memory addresses)
to achive
specialized effects
|
|
The reference
data
types
in
C
Defining
a reference typed variable
- Syntax to
define
a reference variable:
- Meaning of this
definition:
- Define a
reference variable with
name
varName
- The variable
varName
stores
a
reference (= address)
to a variable of the
type
dataType
|
- Example:
int *p; // (1) Defines a variable with name p
// (2) p stores a reference to an int typed variable
|
|
Defining a reference typed variable -
more example
- Examples:
int a, *p; // Defines 2 variables: a and p
// a stores an integer value
// p stores the reference (=address) of
// an int typed variable
float b, *q; // Defines 2 variables: b and q
// b stores a float value
// q stores an reference (=address) of
// a float typed variable
|
- Note:
- The
symbol
* is
also used
as the
multiply operator
(e.g.:
x *
y)
|
- How can
the C compiler (or you)
tell the
difference:
- * used
as
multiply
operation is
a
binary operator
- * used
as
reference
operation is
an
unary operator
|
|
Note on the syntax to
define
reference typed variables
Operations on
reference values
- Recall that:
- A data type
also
provide specialized
operations
on variables/values of that
data type
|
- Example:
operations on
int values:
+ Add 2 integer values
- Subtract 2 integer values
etc
|
- The reference
data type has
2 very powerful
(=
dangerous)
operations:
- The
reference operator
&
- The
de-reference operator
*
|
- C is
known as a
system programming language
because
C programs
can execute
these
low level (=
dangerous) operations
|
❮
❯