C++ Programming Series: Pointers (Part 4, Last)

Too busy! Too busy! Don’t expect regular and weekly posts from me. I am studying medicine.

In the previous post, we discussed that pointers are actually arrays! This post is to discuss more about pointers, arrays and the arithmetic that can be done with them.

We can do addition and subtraction with pointers. Multiplication and division is not possible! Here is the code giving an example of pointer arithmetic (try to understand it deeply):

int array[] = {20, 30, 40, 50, 60};
int* pArray = array;

pArray++;
cout << *pArray << endl; //Prints 30, same as array[1]

pArray++;
cout << *pArray << endl; //Prints 40, same as array[2]

cout << pArray[2] << endl; //Prints 60, same as array[4]
//pArray[4] will give error or garbage value!

pArray -= 2;
cout << *pArray << endl; //Prints 20, same as array[0]

cout << pArray[4] << endl; //Prints 60. same as array[4]

When we increment a pointer of type int, the address is actually shifted by the number of bytes that an integer consists of. Similarly, when we increment a pointer of type string, the address is shifted 4 bytes forward. The opposite happens when we decrements a pointer.

Consider the code below:

string words[] = { "First", "Second", "Third", "Fourth", "Fifth" };

string* pWords = words;

Now, pWords is pointing towards the memory address of words[0] or “First”. If we add one into it:

pWords++; //or pWords += 1;

…then it will be shifted 4 bytes forwards to reach the next string of the array, words. Or in other words, pWords is now, equivalent to &words[1]. Now, if we subtract one from it, it will get to the same address as before.

C++ is a very flexible language. We can add the numbers into the pointer temporarily like pWords + 3. Now, if pWords is equivalent to &words[0], then pWords + 3 would give us the address equal to &words[3]. If we dereference this memory address, we will get the proper string value, “Fourth” like this:

string words[] = { "First", "Second", "Third", "Fourth", "Fifth" };
string* pWords = words;

//Adding temporarily for getting the memory address of the
//fourth element of array and dereferencing that memory
//address to get the value from there
cout << *(pWords + 3) << endl; //Prints "Fourth"

Isn’t that similar to that?

cout << pWords[3] << endl; //Prints "Fourth"

Yes, they are completely the same! What is happening with pWords[3] is the same as what is happening with *(pWords + 3).

That’s it! Your exercise is to find out what is the output of the code below (without checking it out in the compiler):

int array[] = { 1,2,3,4,5,6,7,8,9,10 };
int* pArray = array;

pArray += 5;
cout << pArray[3] << endl; //Output 1
pArray -= 3;
cout << pArray[4] << endl; //Output 2
pArray--;
cout << *pArray << endl; //Output 3
pArray + 2;
cout << *pArray << endl; //Output 4
pArray += 1;
cout << *(pArray + 1) << endl; //Output 5

That is all pointers for you. Congratulations! You learned the pointers! Time to step ahead…

Advertisements

C++ Programming Series: Basic Arithmetic

We discussed many things till now. We discussed about printing the variables, primitive types, user input and some conditional statements. What we should discuss right now is the basic arithmetic.

It is very simple to do primitive calculations with primitive types. We will only work with integers here. But it will be applicable to all other primitive types (of course, ‘char’ is one of them).

Addition:

int a = 10;
int b = a + 5;
cout << b << endl; //Prints 15

int c = 12;
c = c + 5;
cout << c << endl; //Prints 17

int d = a + c;
cout << d << endl; //Prints 27

Subtraction:

int a = 10;
int b = a - 5;
cout << b << endl; //Prints 5

int c = 12;
c = c - 5;
cout << c << endl; //Prints 7

int d = a - c;
cout << d << endl; //Prints 3

Multiplication:

int a = 10;
int b = a * 5;
cout << b << endl; //Prints 50

int c = 12;
c = c * 5;
cout << c << endl; //Prints 60

int d = a * c;
cout << d << endl; //Prints 600

Division:

int a = 10;
int b = a / 5;
cout << b << endl; //Prints 2

int c = 12;
c = c / 5;
cout << c << endl; //Prints 2
//For integers and characters, remainder is discarded, it is the quotient which is left.
//For floats and doubles, there is no remainder and precise division is done.
//For the above division, it will be 2.4 for floats and doubles.

int d = a / c;
cout << d << endl; //Prints 5

I hope that you might have understood all primitive calculations. One can understand it easily if he/she knows algebra. I will not tell you what is algebra because it is not the purpose of this place. Just Google it if you don’t know. However, there are some short hand methods to some of them, which are really useful. Following are those methods:

int add = 10;
add += 5; //equivalent to "add = add + 5;"

cout << add << endl; //Prints 15

int sub = 10;
sub -= 5; //equivalent to "sub = sub - 5;"

cout << sub << endl; //Prints 5

int mult = 10;
mult *= 5; //equivalent to "mult = mult * 5;"

cout << mult << endl; //Prints 50

int divi = 10;
divi /= 5; //equivalent to "divi = divi / 5;"

cout << divi << endl; //Prints 2

There are two more methods that are very confusing but produces neatness in one’s coding style.

int a = 10;
a++; //equivalent to "a = a + 1;" but with some exceptions
++a; //equivalent to "a = a + 1;" but with some exceptions

cout << a << endl; //Prints 12
//Similar is for subtraction

The first one (a++) is called as postfix increment operator and the other one (++a) is called as prefix increment operator. They are not the same and are very much confused with each other. Similar is the case for postfix and prefix decrement operators (i.e, a--, --a). This can be proved by the code below:

int print = 25;

//Method 1.
//cout << print = print + 1 << endl; //This gives error and so, is not possible.

//Method 2.
cout << print++ << endl; //Prints 25

//Method 3.
cout << ++print << endl; //Prints 27

Awww… What’s that? It’s a strange looking code! But let me explain all this. Both postfix and prefix increment and decrement methods can be placed with variables in a complete C++ code sentence as well. Same is not true for a = a + 1; or a = a - 1;. In the 2nd method, print is first printed out and then, incremented by 1. So, it prints 25 and then, its value is changed to 26 i.e print = print + 1; is done after being printed. In the 3rd method, print is first incremented by 1 and then, is printed out i.e print = print + 1; is done before being printed (Remember, 2nd method already changed its value to 26 and so, in 3rd method, 27 is printed out). The equivalent code to this is given below:

int print = 25;

//"cout << print++ << endl;" is equivalent to
cout << print << endl; //Prints 25
print = print + 1;

//"cout << ++print << endl;" is equivalent to
print = print + 1;
cout << print << endl; //Prints 27

There is another thing. We can have remainder instead of quotient, in dividing numbers by a special operator called as modulo operator, represented by %.

int a = 14;
int b = 5;
cout << a / b << endl; //Prints 2, we can do temporary calculations and print them.
cout << a % b << endl; //Prints 4 which is the remainder obtained by dividing a by b.

a %= a; //a is now, 0.
b %= 3; //b is now, 2.

This post is too lengthy to explain all basic arithmetics in C++. But length don’t matters! It is your motivation and inspiration that matters!