Pointer in C/C++ Part – 1

Wanted to start such posts for a long time. After Abhay posted his queries related to pointers, I decided better late than never. I’ll try to post such stuff more frequent. 🙂
Will always be looking forward for your feedback.

What is a pointer?
Pointer is a variable that stores an address as its value.

How to declare a pointer?
To declare a variable as pointer we have to use “*” along with variable name, in its declaration. Following declaration declares variable i as a pointer to integer.

int *i;

“*” in the declaration is associated with the variable name, which means in the following declaration while i is a pointer variable, j is of integer.

int* i, j;

The correct way to declare multiple variables as pointers, in same statement, is by adding “*” before each variable name, as shown below.

int *i, *j;

Another way would be to use typedef to create a new type and use that to declare variables of that type.

typedef int * INT_PTR;
INT_PTR i, j;

Pointer initialization.
While it is ok not to initialize a static and global variable, as they are by default initialized to 0 (zero), it is always better to initialize a local variable, to avoid using the garbage value stored in it. Since a pointer stores an address, it becomes more important to make sure we initialize a pointer to a known value, as accessing a random (garbage) address can result in core dump (linux)/segmentation fault (windows).

Now the question is how do we get a valid address to initialize a pointer variable?

One way is to initialize a pointer with the address of a existing variable, as shown below.

int i;
int *p = &i; // Initialize pointer p, with address of variable i.

Another way is to dynamically allocate memory and use that address to initialize the pointer variable, as shown below.

int *p = (int *) malloc(sizeof(int));
int *q = new int; // Only for C++ programs.

One more way is, this should be used very carefully, to initialize pointer with a “known” address. This known address might be of some memory reserved by OS and documented. In this case we specify the address as a integer and typecast it to pointer type, before storing it in pointer variable, as shown below. When storing integer into a pointer in the following way one must be very careful in doing so becuase after storing that integer into a pointer, any access of that pointer variable will access the contents of the memory address at the integer location.

int *p = (int *) 0x0417; // Address of a byte where MS-DOS stores status of some keyboard keys.

New Year Special – Ghats near Pune

Lavasa Curve

It’s a belief that what ever we do at the start of New Year, we will keep doing that for the rest of year. I wanted to ride out on 31st night but then family and friends had some plans so I decided to party instead.

Couple of days later, it was the first weekend of year and I decided, better late than never. So, I head out to some near by places. Saturday ride was sort of warm up and hence it was just 103km trip, a slow one at that (Ride duration: 2hrs 45min).
Lack of interest in riding on straight roads, I decided to head to near by twisties and sure it was fun even at speeds around 50km/hr (yes, I was running in my bike after a new block piston kit).

Lavasa Curve

The route I took was Chandani Chowk – Mutha – Lavasa – Mutha – Pirangut – Paud – Hinjewadi.

Sunday I decided to head towards Mahabaleshwar, while I was cruising on NH4 at yawning speed I realized I’m not carrying my bike’s original RC book (often documents are checked at Panchgani, only of two wheelers, as if we are criminals while people in cages are all saints). So when I saw a familiar turn off NH4, I jammed my brakes (must have put down a few mm’s of rubber on tarmac) and got off the boring highway. This was the road I have taken for the last time in Jun’03 (when we went for Pulsar Yahoo Group’s Annual meet). This is a lesser known (to travelers) road that takes you to Bhor. Yes, I dumped Mahabaleshwar in favour of Bhor and Varandha Ghat.

Enroute VarandhaEnroute Varandha

The nature of road hasn’t changed much once we cross Bhor. Though its pothole free tarmac almost all the way, there is plenty of gravel on both the edges and in center. I love it, you over cook a curve, run wide and either you will go off the road or can go and kiss mother earth.
Varandha Ghat CurveVarandha Ghat Curve

Since rains have just gone by, water levels are good in the dam en-route.

Backwaters of Neera DamBackwaters of Neera DamBackwaters of Neera Dam

After a small break for hot bhajis and tea in Varandha ghat I headed towards Mahad. It was closing on 5pm and I knew I’ll have to climb back in dark, on my way back. Having climbed Tamhini in dark few days back on CBZ, I wasn’t too worried of taking the same route again.

Varandha Ghat's Valley ViewVarandha Ghat

Once I reached Mahad, I saw Poladpur (to go towards Mahabaleshwar) some 15km’s away and there was no distance to Mangaon (to go towards Mulshi) given, I guessed that Mangaon can’t be too far away so I headed towards it, after few km’s I realized my mistake as it was still some 30km’s away and the sun has started to go behind hills. By the time I reached Mangaon it was 6:15pm and after filling some fuel (yeah twice I have started the climb to Tamhini with bike about to hit reserve – no way was I going to repeat that mistake).

The Mangaon – Vilhe road was almost empty of traffic and in lovely condition. Even though it was dark, it was fun riding in that section filled with twisties. After Vilhe, I decided to stay with couple of cars that were climbing Tamhini but with me not wanting to stress my new engine, mid way thru the ghat one car was too fast for me while the other was too slow, as a result I was left all alone. It was a bit scary and I was remembering only all the scary stories I have heard about this area. Hoping to catch some slow moving vehicle ahead, I kept on riding but I wasn’t that lucky. Once I passed the last dhaba on top of ghat, it was me and my bike all alone. Other than my bike’s headlamp, moon and stars were the only light sources visible. I wanted to stop and click some pictures but wasn’t able to gather enough courage to do so. After riding all alone for some 15min’s I caught up with some traffic as the bad stretch of road has started. Then on it was just a matter of keeping up with them.

Following with vehicles I reached Mulshi lake and what a beautiful view it was, I told myself I’m coming back here in night again, of course not alone :-). As I was feeling hungry, I halted for dinner and then it was a slow cruise till home.

Route: Pune – Bhor – Mahad – Mangaon – Vile – Tamhini – Mulshi – Pune
Distance: 300km’s
Ride Duration: 8hrs 30min

Click on image to view in bigger size.

Panning & Moon’o’graphy – 2

Today I was out again in night sharpening my panning shots. Lday luck was with me and the very first shot came out almost perfect (to my liking). As I was hand holding the camera, I started with ISO1600 and hence some grain can be seen in the image.

Clicked using Canon 70-200 f/4 L lense on 400D, shot @ ISO 1600, 188mm, f/4 and 1/10sec shutter speed.

Motivated by my effort, I got the tripod out and started to practise on the moving objets. Now, I was able to bring the ISO down to 400 without too much blurr as I was no more hand holding the camera. Following is one of the many clics.

Clicked using Canon 70-200 f/4 L lense on 400D, shot @ ISO400, 145mm f/4 and 1/5sec shutter speed.

While I was out, I also decided to capture the white moon, almost in its full shape.

Clicked using Canon 70-200 f/4 L lense on 400D, shot @ ISO100, 200mm f/8 and 1/125sec shutter speed.

Similar topics
Panning – My Debut shots

Panning – My Debut shots

My Pan Shots

Last night as I was done shooting moon from my house balcony, I looked at the well illuminated empty road, some 100m away from me. Tried to click some shots but due to not using a tripod most of them didn’t came well. In between I clicked a zooming car and thats when I decided to try my hand at Panning. Due to low light condition, I had to use higher ISO and really slow shutter speed and hence shots were having too much noise/not sharp enough.

So today morning I decided to venture out once again and here are some of the shots I managed.

My Pan ShotsMy Pan Shots

My Pan ShotsMy Pan Shots

Click on image to view in bigger size.

So how did I clicked them?
I used a slow shutter speed (resulting in larger f-stop/small aperture) and switched between “One Shot” & “AI Servo” (or something like that). Moved the camera along with the moving car/bike. Click and kept moving, when I didn’t moved the camera as I clicked, the object came blurred.

What is Panning?
In simple terms, its about clicking a picture by keeping the moving object in the frame by moving the camera along the object and clicking it at the same time. This will blurr out the background, giving a sense of motion in the image.

For these shots I used my Canon 400D with 70-200 f/4 L lense.

Welcome to 2009

A belated New Year wishes to everyone. Well, I got my New Year gift a bit early. Following is the picture of the gift, my son, born on 21-Dec-08.

My Son

Click on image to view in bigger size.

Just the way everyone make new year resolutions, I have made myself a few and you can see some of them right here in next few days.