Pointer in C | NULL pointer | Example | Concepts | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Thursday, July 2, 2020

Pointer in C | NULL pointer | Example | Concepts | My CS Tutorial

Pointer in C programing | Pointer in C | NULL pointer | Declaration | Example | How to use a pointer | Concepts | Pointer in C language | My CS Tutorial

Pointer in c | My CS Tutorial


POINTERS IN C PROGRAMMING


A pointer is a variable in C that points to a memory location. It does not
directly contain a value like int or float but just a memory direction. One can
reserve some memory on the direction marked by the pointer and use it to
store some values on it, later on these values are indirectly accesed through
the pointer variable.

Declaration


A pointer is declared as follows :
 int *ptr1;

And may be used as in the example to point to a variable of the same type as
the pointer:-

 int i;
 ptr1 = &i;

Here you have to understand the role played by characters '*' and '&' in front
of variables. The character '*' in front of the variable is used to declare a
pointer variable. The character '*' is also used in front of a pointer variable to access and retrieve the value contained by a pointer variable, not the address
(i.e. the address is designated by the name itself without the '*').
The character '&' is used to access the memory address of any variable (note that every variable is stored somewhere in memory).
In the above example, you
are declaring an integer pointer "ptr1" and an integer variable "i". Then you
are forcing the pointer ptr1 to point to the address of the variable "i". Now
both "*ptr1" and "i" refer to the same memory address. Consider the example
below to understand the concept.

Example:-


#include <stdio.h>
main()
{
 int *ptr1;
 int i;
 ptr1 = &i;
 i = 3;
 printf("The value of i is %d\n", i);
 printf("The pointer ptr1 contains the value %d\n",*ptr1);
}

The above program yields the result
 The value of i is 3
 The pointer ptr1 contains the value 3

This illustrates the fact that the pointer ptr1 is pointing to the address of the
variable i. Hence '&' is referred to as the address of operator.

How to Use Pointers?


There are a few important operations, which we will do with the help of pointers very frequently. (a) We define a pointer variable, (b) assign the address of a variable to a pointer, and (c) finally access the value at the address available in the pointer variable.
This is done by using unary operator * that returns the value of the variable located at the address specified by its operand.
The following example makes use of these operations:-

#include <stdio.h>
int main ()
{
 int var = 20; /* actual variable declaration */
 int *ip; /* pointer variable declaration */
 ip = &var; /* store address of var in pointer variable*/
 printf("Address of var variable: %x\n", &var );
 /* address stored in pointer variable */
 printf("Address stored in ip variable: %x\n", ip );
/* access the value using the pointer */
 printf("Value of *ip variable: %d\n", *ip );
 return 0;
}

When the above code is compiled and executed, it produces the following result:-

Address of var variable: bffd8b3c
Address stored in ip variable: bffd8b3c
Value of *ip variable: 20

Concepts of pointer


Pointers have many but easy concepts and they are very important to C
programming. The following important pointer concepts should be clear to any C programmer:-

Concept and Description:-


Pointer arithmetic      :-     There are four arithmetic operators that
can be used in pointers: ++, --, +, -

Array of pointers      :-     You can define arrays to hold a number of
pointers.

Pointer to pointer      :-     C allows you to have pointer on a pointer
and so on.

Passing pointers to functions in c      :-     in C Passing an argument by reference or by address enable the passed argument to be changed in the calling function by the called function.

Return pointer from functions in C       :-      C allows a function to return a pointer to
the local variable, static variable, and
dynamically allocated memory as well.


NULL Pointers 


It is always a good practice to assign a NULL value to a pointer variable in case you do not have an exact address to be assigned. This is done at the time of
variable declaration. A pointer that is assigned NULL is called a null pointer.
The NULL pointer is a constant with a value of zero defined in several standard libraries. Consider the following program:-

#include <stdio.h>
int main ()
{
 int *ptr = NULL;
 printf("The value of ptr is : %x\n", ptr );
 return 0;
}
When the above code is compiled and executed, it produces the following result:
The value of ptr is 0


Pointer in C programing | Pointer in C | NULL pointer | Example | How to use a pointer | Concepts | Pointer in C language | My CS Tutorial


_________________________________________


Please share this post and blog link with your friends.For more programs use this blog.

If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment