Wednesday, August 26, 2015

Pointers and Arrays in C - The story of George St


Arrays and Pointers in C-programming are 2 different types used in a similar manner.

To make an analogy, let’s assume that  there is a street called George street. Buildings are built along the street.


The original plan is that every house takes up the same space (1 unit) and each is assigned a street number. The next house has a street number incremented by 1 from the previous one.


So the street number for houses along the street will be 1, 2, 3, 4 … 249, 250. When people want to build a house or go to a friend’s house to pick up something, they ask for the street number and go to the address accordingly.


This works pretty well for a while.


However, a few years later, this suburb is re-zoned for both residential and business uses.


Since then, warehouses have been built in this area. Unlike normal houses, the warehouse takes up twice as much space as a normal house, which is 2 unit of space.


Buildings are no longer the same size, you can’t assume that each street number represents a house anymore.


As a result, people can’t just ask for an address and use that unit of space without telling the planner what type of the building they are planning to build or visiting.


For example, a warehouse is built in the space which was originally planned for house number 204 and 205.


When people apply to build a warehouse starts from number 204,  the land planner writes down that warehouse is assigned street number 204 and it has the size of 2 unit of space.


So we know that the space for street number 204 and 205 are taken since the warehouse has a size of 2 unit of space.


Next time, they have to start from street number 206 to build something else.

A pointer is like a street number from which you can find the address and the type of building or space you want to use. A pointer must have a type or otherwise you don’t know how much space you can use or how big is the building.


When people want to use some space or to visit a building, they go to the land planner and tell him the street number and the type of building they want to visit or plan to build. The land planner gives them a key with a tag, which has the type of building and the starting street number written on it.


Worth mentioning is that: this key can open any door on George Street.


Once the key and street number is given to you, you can feel free to go to the next door and the next door or the previous door … literally you can go wherever you like and do some doggy things in other people’s houses. But the result may not be what you want.

In some situations, you might want to use 3 warehouses in a row.


You can ask the planner to give you an array of 3 warehouses.


He will give you a key with a tag. On the tag it says: starting address 204; type: warehouse: number of elements: 3.


So you know that you can find the first warehouse by going to the address 204, since each warehouse takes up 2 unit of space, the next one will be at address 206 and the next one will be at 208.


Although there is nothing to stop you from going over the boundary of the space assigned to you, you might not want to do so as there might be serious consequences.


Unlike pointer, the address, type, and number of elements of an array cannot be changed as it is the key for you to find all 3 warehouses.  



No comments:

Post a Comment