Lab 4

Part 1: Command-Line Arguments

Consider the following small program
#include <iostream>
using namespace std;

int main(int argc, char * argv[])
{
for (int i = 1; i<argc; i++)
cout << argv[i] << ' ';
cout << endl;
}

Save this program as argument.cpp and compile it. If we run the program:
$ ./a.out eenie meenie miney moe
eenie meenie miney moe
$

we see that this program echoes the command-line arguments. We have a main function header of the form int main(int argc, char * argv[]); argc is the number of terms in the command-line, including the command name, and argv[] is an array of strings which contains the command name in argv[0], and then all the command-line arguments in argv[1] through argv[argc-1]. The arguments are delimited by blanks and, so, no spaces will appear in any of the arguments passed into the program.

Assignment:

Modify argument.cpp so that it converts the arguments to integers, computes their sum, and then prints out this value.  For example,

$ ./a.out 3 6 10
Sum is: 19
$

Hint: Look Here

Part 2: Pass by Pointer

Definition of a Pointer.

Pointers are a type of variable that allow you to specify the address of a variable. They provide a convenient means of passing arguments to functions and for referring to more complex datatypes such as structures. They are also essential if you want to use dynamic data in the free store area. (That was a free look ahead to the dynamic data topic covered later in these notes.)

You won't always know the specific value in a pointer, but you won't care as long as it contains the address of the variable you are after. You need to declare and initialize pointers just as you would other variables, but there are special operators that you need to use.

Pointer Operators.

Here is a table showing the special characters used in C++ to declare and use pointers.

*
dereference operator,
indirection operator
This is used to declare a variable as a pointer.
It is also used when you want to access the value pointed to by the pointer variable.
&
reference operator,
address-of operator
Use before a variable to indicate that you mean the address of that variable. You'll often see this in a function header where the parameter list is given.
->
member selection operatorThis is used to refer to members of structures

Simple Pointer Use.

We'd better look at some examples to make this clear.

First, we'll declare two ordinary integers, and also pointers to those integers.

int 	alpha 	= 5;
int 	beta 	= 20;
int*	alphaPtr	=  &alpha;
int*	betaPtr	=  &beta;
The characters Ptr in the pointer variable name have no special significance. They are simply a memory aid for the programmer. Let's look more closely at one of the pointer declarations.
int*	alphaPtr	=  &alpha;
The first part int*, tells the compiler to declare a pointer for integers. alphaPtr will be the name of that pointer. In the last part of that statement, &alpha; specifies that the address of the variable alpha is what should be assigned to the pointer variable.

An aside here: It is also permissable to position the asterisk closer to the pointer variable name,
i.e. int  *alphaPtr.   However the convention seems to be moving towards placing the asterisk closer to the datatype.

Try to visualize memory after these declarations, thinking of the pointer variable as not having a particular value, simply links to the variables to which they had been assigned.

Now let's look at a trivial example of how to access this data.
*alphaPtr += 5;
*betaPtr += 5;

After these statements, it is only the contents of the alpha and beta variables that would be changed.

You might say, "What's the big deal here? I could just as easily have written this:"
alpha += 5;
beta += 5;

True, but typically pointers aren't used in such a simple manner. We just showed this to illustrate the use of pointer syntax.

Assignment: Create a function, called swap, that takes two pointers and swaps the values in them. You function should look like this:
void swap(int* arg1, int* arg2)
Use this small main to test this.

void swap(int*, int*); int main() { int a = 2; int b = 4; int *aPtr; int *bPtr; aPtr = &a; bPtr = &b; *aPtr = 5; *bPtr = 10; swap(aPtr,bPtr); cout << *aPtr <<endl; //Should print out 4; cout << *bPtr <<endl; //should print out 2; }
Hint: In your swap function, you probably need to make a temp pointer (to do the swap). Remember what a pointer always needs.
Question (You must answer this): Passing pointers is pass by value. What actually happens to a and b, and why?

Grading:
  1. 40% for Part 1
  2. 60% for Part 2