Skip to main content

Pointers in C for starters

Ask any new computer science student ,"What is the most dreaded topic in C language?". They will reply ,"Still having nightmares about pointers !!!". And I don't blame them. In fact pointers become even more dreaded as the learning curve is really steep for beginner.
So what makes pointers so "evil" ? I personally think its not the pointers but its lack of knowledge about memory that actually kills a new student. Hence I claim "Pointers are innocent ".



Pointers can be easily mastered by any new student, they just need to work with them. It is said that a good programmer will always avoid any type of programming error except that of pointers. Even the mightiest fail with pointers. So don't take it hard on your self , we all make mistakes (specially with pointers and then laugh about it later ).
So whats the deal about these strange looking creatures (pointers that is :P ). To understand it, lets look at the memory in detail.
If you want to visualize memory, its more like a huge set of boxes with a numbers on it. These boxes contain your data and the number on the box helps us getting to these boxes. These "numbers" are called "memory addresses". So everything you create in a C program will be stored in these boxes. every data type require a particular amount of boxes for storage. Like int requires 2 boxes each of 1 Byte in size (assuming int is 2 Byte long)

XYZ
123
100102104
here we see 3 int variables X,Y,Z. with values 1,2,3 and they are stored at memory locations 100,102,104 respectively. As we can see these int require 2 Byte of memory. Easy right?

Q: So where are the pointers?
Ans : In Simple words the pointers will store these Addresses. That's the whole purpose of a pointer,to store the address of other variable (int in this case).
Q: Can Pointers store values?
Ans : Well if the value is an address, YES.
Q : How do I create a pointer?
Ans :Well its very easy, you have to just mention that what data type the pointer will be "pointing".

int* ptr;

Read this statement from right to left. "ptr is a pointer to data type int".
so my ptr pointer will store the address of a int.
But i don't know where my variables are stored. :(
Yeah we know that that's why we use a special operator to get the address of your variable. this operator is "&" operator.
so i can write :-

ptr=&X;

read this statement from right to left. "Address of X gets assigned to pointer ptr". easy right?
Q: So &X gives me the address of X. Will &Y give me address of Y?
Ans : of course it will.
Q : But what is pointer itself?
Ans: Technically speaking pointer itself acts as an special integer that stores addresses.
Q: Pointer is Integer?
Ans : Yes it is.

Q: You mean the pointer itself is stored in memory too?
Ans: Of course like any other variable.

XYZPTR
123100
100102104200
You can see that the pointer itself is stored at address 200. and we can see that pointer contains the value 100 which is the address of variable X. and we say that ptr points to X.

Q: Fine. What can do with pointers now?
 Ans : actually u can access the values "pointed" by the pointers.

Q: I can? how?
Ans: its very easy , we use a special operator "*" called "value at pointer".
So if I say    *ptr  , I mean  "value at ptr".
pointer contains address 100, and the value AT 100 is =1 which is the value of X. (So basically we get the value of the data type we were pointing ).

Summary:-
  • Pointers points to data types.
  • Pointers only contain addresses.
  • "&;" operators is used to get the memory address of a data type.
  • "*" is used by pointers to read value it is pointing.
I hope it helps new C programmers.

Popular posts from this blog

Find nth Prime Number in C++

c++ program to find prime numbers: The problem of finding prime number can be solved by checking all numbers, testing them for prime and then moving ahead. If you want to calculate nth prime. Then this can be done in a brutal way by checking the number one by one. This may sound odd, by there is no easy way then this  for prime numbers (Well Actually there are like Pollard's Rho Algorithm, Number Sieves or Shor's Quantum Algorithm, but we are talking about the one that most people may understand easily). There may be way to pre-calculate the prime numbers but that again is not sufficient. So how can we use c++ to create a program to find prime numbers.

Program to find nth Prime number in C

Objective: To print any Prime number in C.  This task may sound easy but is a very good exercise to help optimize CPU intensive programs. Finding Prime number is a very CPU hogging task and it becomes even more dreaded as the number starts to grow. Lets say we want to find 30001th prime. how will we find it?? Here is the program. Programming logic : The easiest method is to check if the given number N is divisible by any number from 2 to N-1. But it may be noted that we don't need to go beyond the number that is square root of N.  why?? lets have a look. Say we are testing number 49 for prime number. Our objective is to find any number that may divide it. so we start from 2,3,4 etc. As soon as we cross 7 there will be no number that will divide 49, if there was we have got it earlier since it will be smaller then 7.  Get it? another example 64 : 2x32 4x16 8x8 16x4 32x2 as you can see when we pass number 8 we don't need to test as the number that will come ,...