The Point About Variables and Pointers

Some programmers cringe at the mere mention of the word “pointer” because it brings to mind complex, low-level programming techniques that are confounding. Hogwash. Pointers are child play, literally. Watch a 15-month-old carefully and you’ll notice that she points to things she wants, and that’s a pointer in a nutshell. A pointer is a variable that is used to point to a memory address whose content you want to use in your program. You’ll learn all about pointer variables.

Declaring Variables and Objects

Memory is reserved by using a data type in a declaration statement. The form of a declaration statement varies depending on the programming language you use. Here is a declaration statement for C, C++, and Java:

int myVariable;

There are three parts to this declaration statement:

  • Data type Tells how much memory to reserve and the kind of data that will be stored in that memory location

  • Variable name A name used within the program to refer to the contents of that memory location

  • Semicolon Tells the computer this is an instruction (statement)

Primitive Data Types and User-Defined Data Types

abstract data types are used to reserve computer memory. Abstract data types are divided into two categories, primitive data types and user-defined data types. A primitive data type is defined by the programming language, Some programmers call these built-in data types.

The other category of abstract data type, a user-defined data type, is a group of primitive data types defined by the programmer. For example, let’s say you want to store students’ grades in memory. You’ll need to store 4 data elements: the student’s ID, first name, last name, and grade. You could use primitive data types for each data element, but primitive data types are not grouped together; each exists as separate data elements.

A better approach is to group primitive data types into a user-defined data type to form a record. You probably heard the term “record” used when you learned about databases. Remember that a database consists of one or more tables. A table is similar to a spreadsheet consisting of columns and rows. A row is also known as a record. A user-defined data type defines columns (primitive data types) that comprise a row (a user-defined data type).

The form used to define a user-defined data type varies depending on the programming language used to write the program. Some programming languages, such as Java, do not support user-defined data types. Instead, attributes of a class are used to group together primitive data types; this is discussed later.

In the C and C++ programming languages, you define a user-defined data type by defining a structure. Think of a structure as a stencil of the letter A. The stencil isn’t the letter A, but it defines what the letter A looks like. If you want a letter A, you place the stencil on a piece of paper and trace the letter A. If you want to make another letter A, you use the same stencil and repeat the process. You can make as many letter A’s as you wish by using the stencil.

The same is true about a structure. When you want the group of primitive data types represented by the structure, you create an instance of the structure. An instance is the same as the letter A appearing on the paper after you remove the stencil.

Each instance contains the same primitive data types that are defined in the structure, although each instance has its own copy of those primitive data types.

Defining a User-Defined Data Type

A structure definition consists of four elements:

  • struct Tells the computer that you are defining a structure

  • Structure name The name used to uniquely identify the structure and used to declare instances of a structure

  • Structure bodypen and close braces within which are primitive data types that are declared when an instance of the structure is declared

  • Semicolon Tells the computer this is an instruction (statement)

The body of a structure can contain any combination of primitive data types and previously defined user-defined data types depending on the nature of the data required by your program. Here is a structure that defines a student record consisting of a student number and grade. The name of this user-defined data type is StudentRecord:

struct StudentRecord
{
  int studentNumber;
  char grade;
};

Declaring a User-Defined Data Type

You declare an instance of a user-defined data type using basically the same technique that you used to declare a variable. However, you use the name of the structure in place of the name of the primitive data type in the declaration station.

Let’s say that you want to create an instance of the StudentRecord structure defined in the previous section. Here’s the declaration statement that you need to declare in your program:

#include <iostream>
using namespace std;
struct StudentRecord
{
   int studentNumber;
   char grade;
} ;
void main()
{
  StudentRecord myStudent;
  myStudent.studentNumber = 10;
  myStudent.grade = 'A';
  cout << "grades: " << myStudent.studentNumber << " "
       << myStudent.grade << endl;
}

The declaration statement tells the computer to reserve memory the size required to store the StudentRecord user-defined data type and to associate myStudent with that memory location. The size of a user-defined data type is equal to the sum of the sizes of the primitive data types declared in the body of the structure.

The size of the StudentRecord user-defined data type is the sum of the sizes of an integer and a char. As you may know, the size of a primitive data type is measured in bits. The number of bits for the same primitive data type varies depending on the programming language. Therefore, programmers refer to the name of the primitive data type rather than the number of bits when reserving memory. The computer knows how many bits to reserve for each primitive data type.

User-Defined Data Types and Memory

Data elements within the body of a structure are placed sequentially in memory when an instance of the structure is declared within a program. Figure 2-1 illustrates memory reserved when the myStudent instance of StudentRecord is declared.


Figure 2-1: Memory for elements of a structure are placed in sequential memory locations when an instance of the structure is declared.

The instance name myStructure is an alias for the memory address that is reserved for the first primitive data type defined in the StudentRecord structure, which is memory address 1 in Figure 2-1. For the sake of simplicity, let’s say each block shown in Figure 2-1 represents 1 byte of memory and the size of an int is 2 bytes.

Each primitive data type of a structure has its own memory address. The first primitive data type in this example is studentNumber, and its name references memory location 1. The second primitive data type is grade, and its name references memory location 2.

What happened to memory location 1? This can be confusing. Remember that each byte of memory is assigned a unique memory address. Some primitive data types are larger than a byte and therefore must occupy more than one memory address, which is the case in this example with an int. The first primitive data type takes up the first 2 bytes of memory. Therefore, the second primitive data type defined in the structure is placed in the next available byte of memory, which is memory location 2.

Accessing Elements of a User-Defined Data Type

Elements of a data structure are accessed by using the name of the instance of the structure and the name of the element separated by a dot operator. Let’s say that you want to assign the grade A to the grade element of the myStudent instance of the StudentRecord structure. Here’s how you would write the assignment statement:

myStudent.grade = 'A';

You use elements of a structure the same way you use a variable within your program except you must reference both the name of the instance and the name of the element in order to access the element. The combination of instance name and element name is the alias for the memory location of the element.

User-Defined Data Type and Classes

Structures are used in procedure languages such as C. Object-oriented languages such as C++ and Java use both structures and classes to group together unlike primitive data types into a cohesive unit.

A class definition is a stencil similar in concept to a structure definition in that both use the definition to create instances. A structure definition creates an instance of a structure, while a class definition creates an instance of a class.

A class definition translates attributes and behaviors of a real life object into a simulation of that object within a program. Attributes are data elements similar to elements of a structure. Behaviors are instructions that perform specific tasks known as either methods or functions, depending on the programming language used to write the program. Java references these as methods and C++ references them as functions.

Defining a Class

A class definition resembles a definition of a structure, as you can see in the following example. A class definition consists of four elements:

  • class Tells the computer that you are defining a class

  • Class name The name used to uniquely identify the class and used to declare instances of a class

  • Class body Open and close braces within which are primitive data types that are declared when an instance of the class is declared and definitions

  • of methods and functions that are members of the class

  • Semicolon Tells the computer this is an instruction (statement)

the class defines a function that displays the student number and grade on the screen.

class StudentRecord {
    int studentNumber;
    char grade;
    void displayGrade() {
        cout<<"Student: " << studentNumber << " Grade: "
       << grade << endl;
    }
};

Declaring an Instance of a Class and a Look at Memory

You declare an instance of a class much the same way you declare a structure. That is, you use the name of the class followed by the name of the instance of the class in a declaration statement. Here is how an instance of the StudentRecord class is declared:

StudentRecord myStudent;

Memory is reserved for attributes of a class definition sequentially when an instance is declared, much the same way as memory is reserved for elements of a structure. Figure 2-2 shows memory allocation for the myStudent instances of the StudentRecord class. Notice that this is basically the same way memory for a structure is allocated.


Figure 2-2: Memory for attributes of a class are placed in sequential memory locations when an instance of the class is declared.

Methods and functions are stored separately in memory from attributes when an instance is declared because methods and functions are shared among all instances of the same class.

Accessing Members of a Class

Attributes, methods, and functions are referred to as members of a class. You access members of an instance of a class using the name of the instance, the dot operator and the name of the member, much the same ways as you access an element of a structure.

Here is how to access the grade attribute of the myStudent instance of the StudentRecord class and call the displayGrade() method:

myStudent.grade = 'A';
myStudent.displayGrade();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s