FreePrograms


C language is a middle level language. It is a structured language. C language is a case sensitive language. All syntax written in c language is in lower case. C is the basis for C++.
  CONTACT US   ABOUT US   PRIVACY  DISCLAIMER

FREE C PROGRAMS 

What is C C vs C++ vs Java Program Structure Data Types in C Basic Rules of C & C++
Functions in C If, Else Conditions Loops in C Switch Case Arrays
Pointers Structures in C Strings in C Command Line Arguments Type Casting
Linked Lists Recursion Binary Trees Inheritance Multiple Inheritance
Templates File I/O Object Oriented Programming Data Structures in C C interview Questions

Pointers in C : - C Programming Tutorial




A pointer is simply a memory address and its concept is easy to understand.

Let us declare a variable num

int num;

Now let us declare another variable num_ptr

int *num_ptr = &num

num_ptr is declared as a pointer to int. We have initialized it to point to num as shown above.

type * variable name 

Example:

int *ptr; 
float *string;

int * is the notation for a pointer to an int. & is the operator which returns the address of its argument.

The opposite operator, which gives the value at the end of the pointer is *.

In the above c program you can easily find how pointers works.

In other words address is passed by using symbol & and the value is accessed by using symbol *.

.

When we write

*ptr = 5;

it will copy 5 to address pointed by the ptr.

for e.g ptr=&k;

now ptr points to address of k

so now k =5;

when we use the '*' in this way we are referring to the value of that which ptr is pointing to, not the value of the pointer itself.


Pointers and Arrays

Before starting of with arrays please note these important points mentioned below.

n=5;
num=&n;
m=*num;


is same as

n=5;
m=n;

 

Notice how the address and value changes with statements.

                    Name  Address   Value
n=5;               n       15678         5
num=&n;       num   15666     15678
m=*num;        m     15666         5
*num=7;     *num    15666         7

Now lets see arrays .

int x[3]; //x[0],x[1],x[2]

int *no;
no=x; //it will point to x[0]
no=x[2]; //it will point to x[2]
no++; //sets pointer to next element of the array


when we say,

k= r[0];
it is same as
k=*r;

or

k=r[4];
is same as
k=*(r+4);

NULL Pointers in C language

Null pointers are ones that does not point to anything. See the below C program to see how null pointer is declared.

A null pointer should not be confused with an uninitialized pointer: a null pointer is guaranteed to compare unequal to a pointer to any object or function, whereas an uninitialized pointer might have any value. Two separate null pointers are guaranteed to compare equal.

What is dereferencing of a pointer in c language?

int x=12;

int *ptr;

ptr=&x; //give us the address of memory loc. where x is stored, we assign to ptr the address we got. ptr now points to memory loc. where x is stored.

int y;

y=*ptr;//this is pointer dereferencing y now has value of 12

*ptr=24;//x now has value of 24

Void Pointer vs Null Pointer

A void pointer is a pointer which can point to any data type (which of course requires typecasting). Where as a null pointer is used to indicate that it is pointing nowhere.

 

 

COPYRIGHT 2009 ALL RIGHTS RESERVED FREECPROGRAMS.COM