DEV Community

Thilak
Thilak

Posted on

pointers

pointers:

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 using it to store any variable address. The general form of a pointer variable declaration is −

syntax:

type *var-name;

example:
include

int main () {

int var = 20;
int ip;

ip = &var;

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;
}

Types of Pointers

There are different types of pointers which are as follows −

Null pointer

Void pointer

Wild pointer
Enter fullscreen mode Exit fullscreen mode

1.Null Pointer:

You create a null pointer by assigning the null value at the time of pointer declaration.

This method is useful when you do not assign any address to the pointer. A null pointer always contains value 0.

syntax:

Begin.
Declare a pointer p of the integer datatype.
Initialize *p= NULL.
Print “The value of pointer is”.
Print the value of the pointer p.
End.

example:
include
int main() {
int *p= NULL;//initialize the pointer as null.
printf("The value of pointer is %u",p);
return 0;
}

2.Void Pointer:

It is a pointer that has no associated data type with it. A void pointer can hold addresses of any type and can be typecast to any type.

It is also called a generic pointer and does not have any standard data type.

It is created by using the keyword void.

syntax:
Begin
Declare a of the integer datatype.
Initialize a = 7.
Declare b of the float datatype.
Initialize b = 7.6.
Declare a pointer p as void.
Initialize p pointer to a.
Print “Integer variable is”.
Print the value of a using pointer p.
Initialize p pointer to b.
Print “Float variable is”.
Print the value of b using pointer p
End.

example:
include
int main() {
int a = 7;
float b = 7.6;
void *p;
p = &a;
printf("Integer variable is = %d", ( (int) p) );
p = &b;
printf("\nFloat variable is = %f", ( (float) p) );
return 0;
}

3.Wild Pointer:

Wild pointers are also called uninitialized pointers. Because they point to some arbitrary memory location and may cause a program to crash or behave badly.

syntax:
include
int main(){
int *p; //wild pointer
printf("\n%d",*p);
return 0;
}

example:
include
int main(){
int *p; //wild pointer
printf("\n%d",*p);
return 0;
}

Top comments (0)