0% found this document useful (0 votes)
12 views5 pages

Pointers Notes On C Language

Pointers in C are special variables that store the address of other variables, allowing for dynamic memory allocation and manipulation of memory addresses. They enhance program execution speed, reduce complexity, and facilitate access to variables outside functions. Key concepts include the reference operator (&) for obtaining addresses, the dereference operator (*) for accessing values, and the distinction between generic (void) pointers and NULL pointers.

Uploaded by

sanaashaik5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Pointers Notes On C Language

Pointers in C are special variables that store the address of other variables, allowing for dynamic memory allocation and manipulation of memory addresses. They enhance program execution speed, reduce complexity, and facilitate access to variables outside functions. Key concepts include the reference operator (&) for obtaining addresses, the dereference operator (*) for accessing values, and the distinction between generic (void) pointers and NULL pointers.

Uploaded by

sanaashaik5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Page |1

What is a Pointer? What are the advantages and features of Pointers?


Definition:
A Pointer is a special kind of variable in C that holds the address of another
variable. Pointers are also called as memory addresses variable.
Syntax : data_type var_name;
Example : int p;
char p;
Where,  is used to denote that “p” is pointer variable and not a normal
variable.

FEATURES OF POINTERS IN C:
 Pointer in C language is a variable that stores/points the address of another
variable.
 A Pointer in C is used to allocate memory dynamically i.e. at run time.
 The pointer variable might be belonging to any of the data type such as int, float,
char, double, short etc.
 Pointers are used in C program to access the memory and manipulate the address.
 Normal variable stores the value whereas pointer variable stores the address of the
variable.
 The content of the C pointer always be a whole number i.e. address.
 & symbol is used to get the address of the variable.
  symbol is used to get the value of the variable that the pointer is pointing to.
 If a pointer in C is assigned to NULL, it means it is pointing to nothing.
ADVANTAGES OF POINTERS:
 Pointers increase the speed of execution of the program.
 Pointers reduce the length and complexity of the program.
 Pointers enables to access a variable that is defined outside the function.
 Pointers are used to pass the information format and back between function and its
references.
 Pointers save the memory space.
 Pointers are used with data structures.
Rules Of Pointers:
 Only addresses can be stored in Pointer variables. Direct values can not be stored.
(like int, float, char etc.,)
 Pointers are not directly used with operators. Only values in address inside the
pointer variables can be used for operations.
 The datatype of the pointer variable and the normal variable to be pointed should
match.

G PRATHYUSHA Pointers in C Language


Page |2

How the pointers are declared and initialized? (or) How do we access the
Pointer Variable?
Declaration:
Declaration of pointer variable is similar to the declaration of common
variable. A pointer variable should be declared before they are used.
Syntax:
Data type ӿ pointer_variable;
Here, the ‘ӿ’ (asterisk) indicates that the variable is a pointer variable.
Example:
int ӿ ptr;
Here, in this example,
 ptr is the name of pointer variable (name of the memory blocks in which
address of another variable is going to be stored).
 The character asterisk (*) tells to the compiler that the identifier ptr should be
declare as pointer.
 The data type int tells to the compiler that pointer ptr will store memory
address of integer type variable.
 Finally, ptr will be declared as integer pointer which will store address of
integer type variable.
Initializing:
 Pointer Initialization is the process of assigning address of a variable to
a pointer variable.
 Pointer variable can only contain address of a variable of the same data type.
 In C language address operator & is used to determine the address of a
variable.
 The & returns the address of the variable associated with it.
In the above example Pointer ptr is declared, but it not pointing to anything;
now pointer should be initialized by the address of another integer variable.
Initialization of the pointer variable can be done as follows:
int x;
int ӿ ptr; /*Declaration Statement*/
ptr = &x; /*Initialization Statement*/
Here, x is an integer variable and pointer ptr is initiating with the address of x

G PRATHYUSHA Pointers in C Language


Page |3

Write short notes on Reference Operator and Dereference operator.


Reference operators:
 Address of operator (“&”) is known as referencing operator.
 This operator returns the address of the variable associated with the operator.
 For e.g., if we write “&x”, it will return the address of the variable “x’.
 Hence, if we have a pointer “p”, which we want to point to a variable x, then
we need to copy the address of the variable “x” in the pointer variable “p”.
 This is implemented by the statement: p = &x;
Dereference operators:
 Value of operator (“*”) is known as dereference operator.
 This operator returns the value stored in the variable pointed by the specified
pointer.
 For e.g., if we write “*p”, it will return the value of the variable pointed by the
pointer “p”.
 Hence, if we want the value of the variable pointed by the pointer “p” to be
stored in a variable “y”, then the expression can be written as: y = *p;
Write short notes on Generic Pointers / Void Pointers and NULL Pointers.
Void Pointer (or) Generic pointer:
Meaning of generic pointer is a pointer which can point to any type of data.
void pointer in c is known as generic pointer.
Syntax:
void * Pointer_Variable;
Example:
void *ptr;
Here ptr is generic pointer.
Some points about generic pointer in c:
1. We cannot dereference generic pointer.
2. We can find the size of generic pointer using sizeof operator.
#include <string.h>
#include<stdio.h>
void main()
{
void *ptr;
printf("%d",sizeof(ptr));
}
Output: 2
3. Generic pointer can hold any type of pointers like char pointer, struct pointer,
array of pointer etc without any typecasting.
Example:

#include<stdio.h>
void main()
{
char c='A';
int i=4;
void *p;
char *q=&c;

G PRATHYUSHA Pointers in C Language


Page |4

int *r=&i;
p=q;
printf("%c",*(char *)p);
p=r;
printf("%d",*(int *)p);
}
Output: A4
NULL Pointer:
Meaning of NULL pointer is, it is a pointer which is pointing to nothing.
A NULL pointer is a regular pointer that is not pointing to any valid reference or
memory address.
Example: the NULL pointer can be defined in the following ways:
(i) int * p;
p=0 /*p has NULL pointer value*/
(ii) char *ch;
ch=’\0’;
(iii) int * ptr;
ptr=NULL
where NULL is a macro constant which has been defined and stored in the
header files.

[Link]

G PRATHYUSHA Pointers in C Language


Page |5

Write short notes on Pointer Expression?


The pointers can deals with the arithmetic, relational, logical and assignment expressions.
Pointers are not directly used in the expressions, only pointer values are used.
Some of the arithmetic expression used with pointer p1 and p2 are as follows:
int x, y, *p1, *p2, z, a, b, c ;
x = 10;
y = 20;
p1 = &x;
p2 = &y;
z = (*p1) / (*p2);
a = (*p1) + (*p2);
b = (*p1) X (*p2);
c = (*p1) - (*p2);
Note that *p1 / *p2 is invalid, because /* becomes comment statement. So when we use pointer
arithmetic expression we always use brackets.
Some of the relational expressions used with pointers p1 and p2 are:
int q, abc, *p1, *p2;
q = 280;
p1 = &q;
abc = 200;
p2 = &abc;
if(*p1 > *p2)
{
Statement block;
}
Example Program:
#include<stdio.h>
#include<conio.h>
main()
{
int *p1 , *p2 ;
int a , b , sum;
clrscr();
printf(“Enter a , b values :”);
scanf(“%d%d”, &a, &b);
p1=&a;
p2=&b;
sum = *p1 + *p2;
printf(“Addition = %d”, sum);
getch();
}
OUTPUT:
Enter a , b values: 10,20;
Addition = 30

G PRATHYUSHA Pointers in C Language

You might also like