Open E-School

Friday, February 24, 1984

Important things you should know about pointers in C – Part 1

Category: Programming Date: Monday, June 01, 2015

Pointers are variables which hold address of other variables

Pointers are just like normal variables. All the rules which apply to variables apply to pointers. For example every pointer has a name and an address for itself just like other variables. For example look at this code

Int x = 25;
Int *p = &x;

Here x is a variable which has value 25. By rules of programming we know that x will get an address in the RAM. Similarly here p is also a variable and it has the address of x. By rules of programming p will also get an address in the RAM because it is also just like a normal variable. 

Pointers are like normal variables

The size of a pointer is fixed

Pointers are variables which hold the address of other variables or functions. Addresses of all locations in the RAM are integer values. Therefore it won’t be wrong to say that pointers hold integer values. It does not matter whether you have an integer pointer or a character pointer, it will just be holding an address and that address will be an integer.
In the diagram above you can very well see that both x and p are holding integer values. The only difference is that we say that 25 is a value but 500 is an address. But still both of them are integers.

The size of a pointer is the same as that of an integer

As discussed above all pointers hold addresses which are integer values. Therefore a pointer is just like an integer variable. Therefore the size of a pointer is equal to the size of the integer data type.

Size of pointer and integer is same 

The arithmetic operations for pointers behave a little differently

In mathematics if X = 5 then X + 1 will be equal to 6. But not for pointers !

Pointers are identified from their types. Like if we want a pointer to point to an integer variable we will have to create an integer pointer and if we want a pointer to point to a character variable we will have to create a character pointer.

Now let us say that we have an integer variable X in memory and let us suppose that the address of X is 500. We have a pointer P of the type int and it points to X. This means that P has a value 500, which is the address of X.

Int x = 30;
Int *p = &x;

Now when we say p + 1 then what should be the answer? You will think it should be 501. No! But why? Because when we say p + 1 we are asking the pointer to increment 1 unit. Here unit means the type of the pointer. So if the pointer is of type int then by doing +1 we are asking it to increment itself by 1 integer and if the pointer is of the type character then we are asking it to increment itself by 1 character. Therefore by doing p + 1 above we are not incrementing the value of p by 1, we are asking it to increment its value by 1 integer. Therefore the value of p + 1 will be 504 and not 501 (assuming the size of integer to be 4 bytes)

By the same rule when you increment a pointer by some value then just check the data type of the pointer, multiply that value by the size of that data type and add it to the pointer.

Rate this article and help us improve

Please Login to rate
Overall ratings: 0 | Rating: out of 5
Previous Article Next Article

Quick Links



Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..


Login to Open ESchool OR Create your account    Login   SignUp