Array, Pointer and Pointer Arithematic
Array, Pointer and Pointer Arithematic
Objectives: To review students with arrays, arrays declaration, initializing and accessing array
elements, pointers declaration, reference and dereference operators and passing pointers as
arguments to functions.
Enter 10 elements to an array using for loop and finally display it to the user
Declare an integer variable and pointer to an integer variable
Assign address of integer variable to pointer to integer variable
Display address of integer variable and pointer to integer variable
Display value of integer variable and pointer to integer variable
Access array elements via pointer arithmetic
Pointers as call by reference
Array – is a data structure, which stores a fixed size sequential collection of elements of the same
type. An array is used to store a collection of data. It is often useful to think of an array as a
collection of variables of the same type. Instead of declaring individual variables, such as number0,
number1, ..., and number99, you declare one array variable such as numbers and use
numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element
in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest
address corresponds to the first element and the highest address to the last element.
Declaring an array in C++, the programmer specifies the type of the elements and the
number of elements required by an array as follows:
type array-Name [ array-Size ];
This is called a single-dimension array. The array-Size must be an integer constant greater than zero
and type can be any valid C++ data type. For example, to declare a 4-element array called age of
type int, use following statement:
int num[4];
Array initialization is just like a variable. To initialize an array, we provide initializing values
which are enclosed within curly braces in the declaration and placed following an equals sign after
the array name. Following is an example of initializing an integer array at time of declaration.
int num[4]={23,34,65,74};
Array can be initialized using for loop rather than at time of declaration. Following is an
example of initializing an array using for-loop.
int arr[10];
int i = 0;
for(i = 0; i < sizeof(arr); i++)
{
arr[i] = i; // initializing an array
}
To access an array elements, loop can be used to print all or individual elements of an array
such as:
Pointer declaration is shown below. You start by specifying the type of data stored in the
location identified by the pointer. The asterisk tells the compiler that you are creating a pointer
variable. Finally you give the name of the variable.
Data-type *variable-name
int *ptr;
int k;
ptr is the name of our variable (just as k is the name of our integer variable). The '*' informs the
compiler that we want a pointer variable. The int says that we intend to use our pointer variable
to store the address of an integer.
Referencing means taking the address of an existing variable (using &) to set a pointer variable.
In order to be valid, a pointer has to be set to the address of a variable of the same type as the
pointer, without the asterisk. For example in following code p1 references c1 as shown
int c1;
int *p1;
c1 = 5;
p1 = &c1;
Dereferencing means accessing the variable value stored at a memory address. It is also
called as indirection operator. For example in following code p dereferencing i as shown below
int i = 5;
int *p;
p = &i;
*p = 7; //*p returns the variable stored at the memory address
stored in p, which is i. i is now 7
Array & Pointer: Array name by default, represents the address of the first element of the array and
hence can be directly assigned to a compatible pointer variable. As the array elements are stored in
contiguous memory locations, simple pointer arithmetic may also be used to access all array
elements using the same pointer and without the need to use index values.
int array[5]={1,2,3,4,5};
int* ptr=array;
cout<<”first element of the array = “<<*ptr<<endl; //evaluates as *(ptr + 2*0)
cout<<”Third element of the array = “<<*(ptr+2)<<endl; //evaluates as *(ptr + 2*2)
cout<<”Fifth element of the array = “<<*(ptr+4)<<endl;
Pointers like call-by-reference also can be used to modify one or more variables in the caller or
to pass pointers to large data objects to avoid the overhead of passing the objects by call-by-
value. We can use pointers and the indirection operator to simulate call-by-reference. When
calling a function with arguments that should be modified, the addresses of arguments are
passed. This is accomplished by applying the address operator (&) to the name of the variable
whose value will be modified
Step 3: Save file again, build and run it to see output as shown in figure below
Figure 7 (b): Displaying array elements
Step 4: Over-write file Lab2 with the following code as shown in figure below
Step 5: Save file again, build and run it to see output as shown in figure below
Figure 8 (b): Pointer variable referencing & dereferencing
Step 6: Over-write file Lab2 with the following code as shown in figure below
Step 8: Save file again, build and run to see output shown in figure below
Figure 9 (b): Pointers as call by reference
Declare and initialize an array of size taken from user as input, display array values on
screen and find sum of array elements and finally display the summation result.
Declare an array of size 8. Receive all the elements from the user as input using for-
loop. Display the entered elements the array in reverse order.
Use an int pointer and an int variable and displays the variables address.
Receive input from user, store it in integer variable, display what the user entered, via
the variable’s address.
Create a float array of 10 elements. Using pointer arithmetic, display the elements at odd
numbered positions.
Create a class named “Car” with attributes xPosition, yPosition, speed. The class should
have methods such as “accelerate”, “decelerate” to increment and decrement the speed
of the car while for change in xPosition & yPosition, there should be methods such as
moveForward, moveBackwards, turnLeft and turnRight. An extra method “currState”
should display all the data members of the object.
Create an array of 10 cars, randomly assigning values to xPosition, yPosition using the
parameterized constructor of the Car class. Using a loop of 100 iterations, update and
display the state of all the cars. For updating the status, randomly select a car and then
randomly move it in 1 of the 4 directions. The loop should also notify if any car collides
with another car.
Note:
1. for randomly selecting a car, use rand() function to generate a random value
between 0 to 9. Use that number for the index value to read the car object.
2. For randomly selecting a direction to move, use rand() function to generate a random
value between 1 and 4. Use Switch statement to call the move function associated
with the value.