Tuesday, November 29, 2011

Understanding Pointer Variables in C++

Understanding Pointers Variables


A pointer in C++ is an address of a memory location where data is stored. Understanding pointer variables in in C++ provides an opportunity to advance knowledge on how programs access data in memory. A pointer in in C++ provides an indirect way of accessing data in memory. When a variable is declared, the compiler reserves and assigns it a portion of space in memory. This portion of memory is known to the computer by the address associated to it. Knowing the memory address of the variable helps us in understanding pointers in in C++. Once we know this address, we can utilize it using the a reference or a pointer. A pointer variable is defined to point (to store the address of) to the data stored at a specific memory.


Understanding how to Declare and Initialize a Pointer in C++


To understand how to initialize and declare a pointer variable in c++, start with the data type followed by a dereference operator (*) and the name of the variable. Once a C++ pointer has been declared, we assign it with the address of the variable to store. By default, it does not matter how you append the *, as long as it is between the data type and the pointer variable. The name of a pointer variable, must comply with the rules that govern every variable. The value of a declared pointer variable in c++ is the address to which it points. Follow the example below.
      int    *ptr1;                    // pointer to an int
      float   *ptr2;                    // pointer to a float 

Assume we have two variables age and height,we can now store the address of each in our pointer variables  ptr1 and ptr2 respectively as follows:

                ptr1 = &age;       //ptr1 points to age;

                ptr2=&height     //ptr2 points to height;

Read the whole chapter on understanding pointer variables in C++

Wednesday, November 2, 2011

Introduction to C++ Arrays

Understanding C++ Arrays


C++ Arrays help us understand how to store large data. C++ arrays consists of set of objects (called its elements), all of which are of the same type and are arranged contiguously in memory. understanding C++ Arrays provide you with a convenient way to manipulate storage of large amounts of data from within programs. C++ Arrays allow for setting aside a group of memory locations which we can then manipulate as a single entity, at the same time enabling us direct access to any individual component.

C++ Array elements are grouped under one symbolic name or identifier. All C++ array elements are positioned next to one another in the memory and are indexed in the order they are arranged beginning from position zero. The index of each C++ array element is used to access that element. The number of elements in a C++ array is called its dimension. The dimension of a C++ array is fixed and predetermined, it cannot be changed during program execution.

A C++ array is declared as follows:
 
DataType ArrayIdentifier [NumberOfElements]

Suppose we want to store the grades of 10 students in a class. We can declare and initialize C++ array elements of grades array as follows;

int grades[10]={35,60,55,50,65,80,70,75,45,85}

Read the whole Chapter on C++ Arrays