The value of the arggc argument is the count of arguments. This includes
the name of the program. So in this case, the value of argc is 2.
The argv argument is more complicated.. It represnts an array
of pointers to arrays of characters. Here is how it works. A string is
stored as an array of characters. The last element in the array a NUL character.
This is used to mark the end of the string. If we were to make a pointer
to this array, it would be a character pointer because the type of the
things in the array is character. In general, if you have an array of some
type, a pointer to this array is oft he type a pointer to the type.. For
example. if we have an array on integers like this,
int example_array[10];
To make a pointer to thsi array we do
int *ptr;
Think of it as if we make a pointer to the first thing in the array.
Now if we wanted an array of pointers to character arrays, we first
look at the type of the array.
char str[];
So a pointer to one of these would be
char *sptr;
Now if we wanted to make an array of these it would be
char * carray[];
This is read as an array of character pointers. This is alternate way
of specifying the argv argument.
Lets look at how the two declarations can be the same.Remember that
the name of an array is a pointer to the array. So if we have a characater
array
char joe[];
then joe by itself is a pointer to an array of characters. We know
that the type of this is
char * joe;
so
char * *argv; is read as a pointer (*argv) to character pointers.
Since character pointers can point to arrays, we can also write this as
char * argv[];
So back to the arguments to our program. argv is an array of character
pointers. Each pointer points to a string containing one of the arguments.
Since there are two arguments, the first element, number 0, points to the
array containing the name of the program, str. the second element
of argv, number 1, is a pointer to the array containing the string hello.
Since argc tells us how many elements there are in the argv array, we
can check each of the arguments. In our case, we will make a pointer to
the string on the command line.
-
Variable declaration
The declaration of a variable tell the compiler what kind of a thing
it is. It doesn't always means the compiler will actually make a vriable.
That can happen later. The definition of a variable is when the
memory for it is actually made. Often these happen at the same time.
In our case,
char *instr,*ptr;
int len=0;
instr=argv[1]; /* the first argument, the string to
work on */
These lines both declare and define the variables. The first line
makes two character pointers, The second creates an integer. The third
line assigns the value of argv[1] to instr. Since argv is an array of character
pointers, the types match up. So now, the variable instr points to the
array containing the string we passed to the program.
Lets look at declarations some more.
The main types in C++ include these, integer floating point number,
character and pointer.
Integer
These are indicated by the keyword int. These are ordinary integer values.
You can modify the integer by using these other keywords.
signed |
The default case, the variable can be positive or negative |
unsigned |
This means the variable can only be positive, allows somewhat larger
numbers |
short |
This used to mean the variable was half the size of an int, usually
now it means the same as int |
long |
This makes an integer that is twice as long as an int |
Some combinations of these are allowed. An example would be
unsigned long int joe;
Character
These are one byte (typically) variables that contain the ASCII code for
a character. For example, if a char variable is assigned the letter A,
char x='A'; // note the single quotes
The integer value would be 65. You can do arithmetic on characters.
So after doing
c=c+1;
C would contain 66, which is the letter 'B'. There are special characters
and here is how you denote them
'\n' |
A newline character |
'\t' |
tab |
'\0' |
NUL end of string marker |
'\\' |
a backslash |
'\'' |
a single quote |
'\nnn' |
Any ASCII character, the nnn is the octal number of the character |
Boolean
Boolean variables are either true or false. There is a convention
in C++ that the number 0 is false and all other numbers are true.
But in most cases it is better to use the actual Boolean variables rather
than the integer convention.
So given
bool flag;
This can be set to true or false;
flag = false;
And used in if statements
if(flag) {do true stuff }
Floating point numbers
These are used to represent numbers with decimal points.
float f=3.5;
Be careful of mixing floats and ints together. In the expression
int i=12;
float f=3.4;
print(i/f);
will give 4, rather than 3.52.
This is because, when the types are mixed, everything is made into
an integer. And when a float is converted, it is truncated. So 3.4 becomes
3. To fix this, you have to tell the compiler to make the integers into
floats like this.
print ( (float) i / f);
The (float) is a cast. This temporarily changes the thing after it into
the type in the parentheses. So since i is an integer, this changes the
integer value in i (12) into a floating point value (12.0) and then the
expression works like you would expect.