Вы находитесь на странице: 1из 3

Submitted By Manish Kumar (hellomanish890@gmail.

com)
Downloaded From : www.c4cpp.co.nr

Introduction
Definition: Pointer is a variable which stores address of another variable.
To understand this definition properly, let us separate it into two statements:
1. Pointer is a variable.
2. It stores address of another variable.
Consider the first statement, it means that a pointer is a variable like any
other variable such as i in the statement int i, f in the statement float f, c in
the statement char c etc. Now consider the second statement, it states
pointer stores address of another variable. And this is the difference between
pointer and an ordinary variable. An ordinary variable stores value such as a
whole number, real number, a character etc, where as a pointer stores
address of another variable.

How pointers are declared and used?


See the following statement in C programming language:
int i = 327;
say 3843 is the address (or say location number) in the memory where the
value 327 is stored. Now to retrieve this value from the memory, we can use
pointer.
How? See this..
int * ptr; this is pointer declaration. here * is read as value at address, it
denotes that variable ptr is not an ordinary variable but it is a special
variable i.e. it is a pointer variable
ptr=&i; here & is address of operator. It assigns address of i to ptr
Get the value of the variable ptr (address of i) by using the statement
printf(%u, ptr);
Answer:
3843

If you want to see value stored at the address use following


printf(%d, *ptr);
Answer: 327

What is the use of pointers?


All the beginners who are learning pointers wonder some time or the other,
what is the use of pointers? The answer is, pointers are primarily used for
constructing references, as well as in passing the data between different
parts of a program.
Pointers are used in languages such as C, Free BASIC, C++, Pascal,
FORTRAN, and most assembly languages.

Let us discuss pointers with reference to C programming


language.
1. Pointers can be dereferencing to access the data stored at the address
pointed to, or to invoke the pointed-to function.
2. Pointers can be manipulated using normal assignments and also pointer
arithmetic.
3. The run-time representation of a pointer value is typically a raw memory
address, but since a pointer's type includes the type of the thing pointed to,
expressions including pointers can be type-checked at compile time.

Pointers are used for many different purposes in C.


1. Text strings are commonly manipulated using pointers into arrays of
characters.
2. Dynamic memory allocation is performed using pointers.
3. Pointers to functions are useful for callbacks from event handlers.
A null pointer is a pointer value that points to no valid location (it is often
represented by address zero).
Dereferencing a null pointer is therefore meaningless, typically resulting
in a run-time error.

Null pointers are useful for indicating special cases such as no next
pointer in the final node of a linked list, or as an error indication from
functions returning pointers.
Void pointers (void *) also exist and point to objects of unknown type, and
can therefore be used as "generic" data pointers. Since the size and type
of the pointed-to object is not known, void pointers cannot be
dereference, nor is pointer arithmetic on them possible, although they can
easily be (and in fact implicitly are) converted to and from any other
object pointer type.

Вам также может понравиться