Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/articles/c-tutorials/)
-   -   Introduction to Pointers in C (http://www.go4expert.com/articles/introduction-pointers-c-t27959/)

lionaneesh 6Mar2012 13:36

Introduction to Pointers in C
 
I am doing C programming from a couple of years ago and finally I convinced myself and grabbed all my guts to write an article on Pointers. Now, one could easily question Why? Basically, Pointers are the most useful part of C and usually the most difficult to understand.

Note: I don't want to discourage my readers, that learning about pointers can be a difficult but that's what most of the programmers say and according to me its the most exciting part of C Programming.

For the scope of this tutorial we'll only be looking at how to declare a pointer? , and the most basic pointer operators.

What is a Pointer



A pointer is a variable that contains the address in memory of another variable. We can have pointers for any data type in C, for example a long pointer , int pointer, char pointer etc. We also have a void pointer which does not have a type, thus it can be used to point to any data-type or even any kind of data, for the scope this tutorial we'll be not looking into any operations on void pointers. Void pointers are a special type of pointers in C and they need to be used in a different way which is out of the scope of this tutorial.

Operators



The unary operator '&' gives the adress of a variable.

Demonstration :-

Code:

#include<stdio.h>

int main()

{

int x = 0;

printf("Address of x: %p\n", &x);

}

Output :-

Code:


Address of x: 0xbf912a5c

Which would obviously give out a different address on different executions (Due to ASLR).

The dereference operator i.e * gives the contents of the object pointed by a pointer.

Declaring a pointer :-

To declare a pointer we follow the following format :-

Code:


[TYPE] *[NAME]

of course that's not a strict format and there are a number of ways to declare a pointer but for the scope of this tutorial, we'll be sticking to this one.

example :-

Code:


int *ptr

The above code declares an int pointer named “ptr”.

Note: Same as any C variable pointer also points to a garbage value (i.e address in case of pointers), and should always be initialized before using. Using uninitialized pointers can cause reading memory from an arbitrary address which will eventually result in a Segmentation fault i.e Invalid Reads or Writes depending on the usage.

Working with Pointers



example.c
Code:

#include<stdio.h>

int main()

{

int *ptr;

int x = 0;

ptr = &x;

printf("x : %d\n", *ptr);

}

Note: We must associate the pointer to its same type, i.e A long pointer should be associated with a long variable, and not with any other data type.

Consider a set of strict boxes, in which only a specific type of item can be contained, for instance a square box cannot hold a rectangular object. Similar is the case with the pointers. (Again I am not taking Void pointers into consideration as of this tutorial.)

Before reading the explanation below or before compiling, try to figure out what should be the output of the above program.

Output :-
Code:


x : 0

Explanation :-


5th line: Here we declare an int pointer named ptr.
6th line: Here we declare an int variable x and initialise it to 0.
7th line: Here we use the unary operator '&' to get the address of x and store it in ptr.

Now the pointer ptr contains the address of x, now the value of x can be accessed using the dereferencing operator.

9th line: Here we use the derefrencing operator which gives the value of the variable pointed by our pointer i.e x.

Now that might be a little difficult to grasp at first but believe me its easier than it looks.

Exercises :-

In this tutorial we were inroduced to basic usage of pointers in C, now I'd like the viewers to write some basic programs to test their knowledge.
  • Write a program to print the value of a long variable using pointers.
  • Write a program to print the value of a char variable using pointers.

Scripting 8Mar2012 02:32

Re: Introduction to Pointers in C
 
Good tut man! Keep it up!

lionaneesh 12Mar2012 00:23

Re: Introduction to Pointers in C
 
Quote:

Originally Posted by Scripting (Post 93173)
Good tut man! Keep it up!

Thanks :)

alaziaonline 14Mar2012 17:31

Re: Introduction to Pointers in C
 
you shared a very informative information here thanks a lot for sharing

Davisandrian 15Mar2012 12:42

Re: Introduction to Pointers in C
 
Read your tut, its really helpful. Thanks for spreading.

samantha1230 28Mar2012 17:19

Re: Introduction to Pointers in C
 
Me too get every time confused with pointers. Thats good at-least after long time you successfully understand about pointers. Theoretically they can be understood easily but at time of practical I became dumb.

honeylamb23 30Mar2012 17:56

Re: Introduction to Pointers in C
 
C programming is the hardest programming language I've tried..

tinytomhanks 26Apr2012 03:01

Re: Introduction to Pointers in C
 
I am also start using c, i find it very resourceful. your telling style is very good.

sandeep552 8May2012 18:25

Re: Introduction to Pointers in C
 
Nice one, thanks for introduce pointer.

tinytomhanks 15May2012 04:35

Re: Introduction to Pointers in C
 
I feel happy to be here again, well as I know pointer is nothing but a variable which contain the address of others variables.


All times are GMT +5.5. The time now is 10:53.