Table of Contents

Project: Exploring Pointers and Arrays

A project for CPROG by Lauren Burzynski during the Fall '11.

This project was begun on November 15th, 2011 and is anticipated to take a week.

Objectives

The purpose of this project is to challenge my knowledge of pointers and arrays. It is essential to understand how to properly use pointers and arrays, since these two topics are used so frequently in C and C++. I hope to accomplish a successful program that properly incorporates pointers and arrays within it, learn how to define pointer variables, and learn what types of variables a pointer points to.

Prerequisites

In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

Background

While working on this project, I am attempting to extend my knowledge about pointers and arrays, and also successfully create and compile a program that properly uses pointers and arrays. I am attempting to extend my knowledge on this topic since it has given me a bit of trouble in the past, and I would like to focus more of my time and effort into these topics so that I can learn how and why to use pointers and arrays. To declare pointer variable we need to use * operator before the variable and after data type. A pointer can only point to variable of the same data type. Arrays in C are used to store data under a single variable name. It is common to think of an array like a list of variables of the same type (such as int, float, or char).

Scope

I am anticipating writing and compiling a successful program that deals heavily with pointers and arrays. Within this program, I want to add comments next to lines of code that will give another user a clearer view of what is happening, how and why I wrote that particular line, etc. Within this program, I wish to address how to properly define a pointer, declare what types of variables the pointer will be pointing to (this will be seen within the comments section of the program), among other discoveries I hope to make while devoting more time and effort into pointers and arrays.

Attributes

Procedure

First, I decided to write two smaller programs to successfully demonstrate pointers and arrays. I felt doing it this way was a bit easier for me to understand, by taking the two topics seperately and focusing on each of them independently. The first program I wrote consists of me demonstrating how to write a simple array of various prices on a shopping list. What I did was write two for statements: one statement lists all of the original prices, and the second statement lists all of the prices including an 8% sales tax. Second, I chose to write a smaller program using a pointer with a function to demonstrate how to double the value of an integer. In the beginning of the code, I declare a “DoubleIt” function, then I give a set value to an integer (which is 2), then I call on the function and get the result of '4'.

Code

This first code I wrote based on a shopping list: I listed 5 items (using an array) at their original cost, then added 8% sales tax. The first for statement lists the original prices of the items, the second for statement lists the prices of the items after sales tax.

#include <stdio.h>
#define  ARR_SIZE  5
 
int main(void)
{
  static float const prices[ARR_SIZE] = { 1.99, 1.49, 13.99, 50.00, 109.99 };
  auto float total;
  int i;
 
  for (i = 0; i < ARR_SIZE; i++)
  {
    printf("price = $%.2f\n", prices[i]);
  }
 
  printf("\n");
 
  for (i = 0; i < ARR_SIZE; i++)
  {
    total = prices[i] * 1.08;
    printf("total = $%.2f\n", total);
  }
 
  return(0);
}

Here is another small code I wrote focusing on pointers, and how to use a pointer with functions. In this particular program, I just doubled the value of the original integer using a pointer:

#include <stdio.h>
void DoubleIt(int *num)
{
*num*=2;
}
int main()
{
   int number=2;
      DoubleIt(&number);
        printf("%d\n",number);
return 0;
}

Execution

This is the outcome of the first program listed above (regarding the sales prices and the sales tax):

lab46:~/src/cprog$ ./project2
price = $1.99
price = $1.49
price = $13.99
price = $50.00
price = $109.99

total = $2.15
total = $1.61
total = $15.11
total = $54.00
total = $118.79
lab46:~/src/cprog$

This is the outcome of the second program I wrote (listed above) about using pointers and doubling the value of the integer:

lab46:~/src/cprog$ ./project2.1
4

Reflection

When I began this project, I wanted to leave it open-ended as to what my program would be accomplishing (aside from properly using arrays and pointers). I did this because I had a feeling I was going to want to do more than one program to demonstrate these topics. I knew going into this project that pointers were a little trickier for me to understand, so I wanted to set aside this project to really focus on these topics so that I could have more practice. As I started my research, I realized I could use arrays and pointers with loops and functions, among other things. I decided to break up my program into two seperate codes so I could utilize several different attributes for this particular project, which was not anticipated when I wrote up my project proposal. After completing this project, I realized that was a good idea because I was able to practice more with functions and for statements, along with demonstrating how to use pointers and arrays.

References

In performing this project, the following resources were referenced: