Quantcast
Channel: C Programming – I'm Programmer
Viewing all articles
Browse latest Browse all 30

Pointer In C Language

$
0
0

 

Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without using pointers.

Pointer is a variable that represents the location of a data item, such as variable or an array element in c language .

In C Pointer is used to allocated memory dynamically i.e. at run time . C Pointer is a variable that stores the address of another variable . The variable might be any of the data type such as int, float, char, double, short etc.

pointer store address of another variable .

In c language , The address operator is ‘&’ and indirection operator is ‘*’ are unary operators and they are the members of the same precedence group as the other unary operators. The address operator (&) must act upon operands that associated with unique address, such as ordinary variable or single array element. Thus the address operators cannot act upon arithmetic expressions. The indirection operator can only act upon operands that are pointers . for example pointer variables.

C Pointer declare by * operator is use in c language programming.

The value of null pointer is 0 in c language programming . The size of any pointer is 2 byte (for 16 bit compiler) in c language . Always C pointer is initialized to null, i.e. int *p = null in c language programming .

So, A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before you can use it to store any variable address. The general form of a pointer variable declaration is:

datatype *variable name;

 

Example :

1 #include<stdio.h>
 2 #include<conio.h>
 3 void main()
 4 {
 5      int *ptr, p;
 6      clrscr();
 7      p = 50;
 8      ptr = &p;  /* address of p is assigned to ptr */
 9
10     printf(“%d”, *ptr)  /* display p’s value using ptr variable */
11
12     getch();
13 }

Output :
10

 

NULL Pointers in C :

It is always a good practice to assign a NULL value to a pointer variable in case you do not have 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

On most of the operating systems, programs are not permitted to access memory at address 0 because that memory is reserved by the operating system. However, the memory address 0 has special significance; it signals that the pointer is not intended to point to an accessible memory location. But by convention, if a pointer contains the null (zero) value, it is assumed to point to nothing.

To check for a null pointer you can use an if statement as follows:

 

if(ptr) /* succeeds if p is not null */
if(!ptr) /* succeeds if p is null */

The post Pointer In C Language appeared first on I'm Programmer.


Viewing all articles
Browse latest Browse all 30

Trending Articles