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
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
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
Notice how the address and value changes with statements.
Name Address Value
Now lets see arrays .
int x; //x,x,x
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?
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.
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