0% found this document useful (0 votes)
61 views28 pages

TCS 305-As04

Pointers in C store the address of another variable. They allow accessing and modifying the value of the variable being pointed to. Pointers are declared with a data type followed by an asterisk, and are initialized by using the address of operator (&) before a variable. Functions can also be accessed through pointers by declaring a function pointer type and assigning it the address of a function.

Uploaded by

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

TCS 305-As04

Pointers in C store the address of another variable. They allow accessing and modifying the value of the variable being pointed to. Pointers are declared with a data type followed by an asterisk, and are initialized by using the address of operator (&) before a variable. Functions can also be accessed through pointers by declaring a function pointer type and assigning it the address of a function.

Uploaded by

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

Pointers in C

✓The pointer in C language is a variable which stores the address of


another variable.
✓This variable can be of type int, char, array, function, or any other
pointer.
✓The size of the pointer depends on the architecture.
✓However, in 32-bit architecture the size of a pointer is 2 byte.

Consider the following example to define a pointer which stores the


address of an integer.
int n = 10;
int *p
p = &n;
*p→n
Variable p of type pointer is pointing to the address of
the variable n of type integer.
Declaring a pointer
The pointer in c language can be declared using * (asterisk
symbol). It is also known as indirection pointer used to
dereference a pointer.
data_type * pointer_variable_name;
int *a;// pointer to int
char *c;// pointer to char
•data_type is the pointer’s base type of C’s variable types
and indicates the type of the variable that the pointer
points to.
•The asterisk (*: the same asterisk used for multiplication)
which is indirection operator, declares a pointer.
Initialize a pointer
After declaring a pointer, we initialize it like standard variables with
a variable address. If pointers in C programming are not
uninitialized and used in the program, the results are unpredictable
and potentially disastrous.
To get the address of a variable, we use the ampersand
(&)operator, placed before the name of a variable whose address
we need. Pointer initialization is done with the following syntax.
Pointer Syntax
pointer = &variable;
An example of using pointers to print the address and value is given below.

As you can see in the above figure, pointer variable stores the address of number variable, i.e.,
fff4. The value of number variable is 50. But the address of pointer variable p is aaa3.
By the help of * (indirection operator), we can print the value of pointer variable p.
#include<stdio.h>
int main()
{
int number=50;
int *p;
p=&number; //stores the address of number variable
printf("Address of p variable is %x \n",p); // p contains the address of the number
printf("Value of p variable is %d \n",*p); //*p stores the value of the pointer
return 0;
}
OUTPUT :-

Address of p variable is:- fff4


Value of p variable is :-50
Address Of (&) Operator
The address of operator '&' returns the address of a variable. But, we need to use %u to
display the address of a variable.
#include<stdio.h>
int main()
{
int number=50;
printf("value of number is %d, address of number is %u",number, &number);
return 0;
}
Output

value of number is 50, address of number is fff4


NULL Pointer
A pointer that is not assigned any value but NULL is known as the NULL
pointer. If you don't have any address to be specified in the pointer at the
time of declaration, you can assign NULL value. It will provide a better
approach.

int *p=NULL;
In the most libraries, the value of the pointer is 0 (zero).
#include <stdio.h>
int main()
{
int *p = NULL; //null pointer
printf(“The value inside variable p is:\n%x”,p);
return 0;
}
Output is:= 0
Pointer Program to swap two numbers without using the 3rd variable.
#include<stdio.h>
int main()
{
int a=10,b=20,*p1=&a,*p2=&b;
printf("Before swap: *p1=%d *p2=%d",*p1,*p2);
*p1=*p1+*p2;
*p2=*p1-*p2;
*p1=*p1-*p2;
printf("\nAfter swap: *p1=%d *p2=%d",*p1,*p2);
return 0;
}
C Pointers & Arrays with Examples
Traditionally, we access the array elements using its index,
but this method can be eliminated by using pointers.
Pointers make it easy to access each array element.
#include <stdio.h>
int main()
{
int a[5]={1,2,3,4,5}; //array initialization
int *p; //pointer declaration
p=a; // Assign the address of a[0] to p
printf("Printing the array elements using pointer\n");
for(int i=0;i<5;i++) //loop for traversing array elements
{
printf("\n%x",*p); //printing array elements
p++;
}
return 0;
}
Array of Pointers in C

Just like we can declare an array of int, float or char etc, we can also declare
an array of pointers, here is the syntax to do the same.

Syntax: datatype *array_name[size];


//how to use an array of pointers.

#include<stdio.h>
int main()
{
int *a[3];
int a = 10, b = 20, c = 50, i;
a[0] = &a;
a[1] = &b;
a[2] = &c;
for(i = 0; i < 3; i++)
{
printf("Address = %d\t Value = %d\n", a[i], *a[i]);
}
return 0;
}
OUTPUT=
Address = 387130656 Value = 10
Address = 387130660 Value = 20
Address = 387130664 Value = 50
C Function Pointer
As we know that we can create a pointer of any data type such as int, char, float,
we can also create a pointer pointing to a function. The code of a function always
resides in memory, which means that the function has some address. We can get
the address of memory by using the function pointer.
#include <stdio.h>
int main()
{
printf("Address of main() function is %p",main);
return 0;
}
Declaration of a function pointer
we have seen that the functions have addresses, so we can create pointers
that can contain these addresses, and hence can point them.

Syntax of function pointer

return type (*ptr_name)(type1, type2…);


FOR EXAMPLE:
int (*ip) (int);
In the above declaration, *ip is a pointer that points to a function which
returns an int value and accepts an integer value as an argument.
float (*fp) (float);
In the above declaration, *fp is a pointer that points to a function that
returns a float value and accepts a float value as an argument.

We can observe that the declaration of a function is similar to the declaration


of a function pointer except that the pointer is preceded by a '*'. So, in the
above declaration, fp is declared as a function rather than a pointer.

Till now, we have learnt how to declare the function pointer. Our next step is
to assign the address of a function to the function pointer.
float (*fp) (int , int); // Declaration of a function pointer.
float func( int , int ); // Declaration of function.
fp = func; // Assigning address of func to the fp pointer.

In the above declaration, 'fp' pointer contains the address of the 'func' function.
Calling a function through a function pointer
We already know how to call a function in the usual way. Now, we will see how to call a function
using a function pointer.
Suppose we declare a function as given below:
1. float func(int , int); // Declaration of a function.
Calling an above function using a usual way is given below:

1. result = func(a , b); // Calling a function using usual ways.


Calling a function using a function pointer is given below:

result = (*fp)( a , b); // Calling a function using function pointer.


result = fp(a , b); // Calling a function using function pointer, and indirection
#include <stdio.h>
int add(int,int); //FUNCTION DECLARATION
int main()
{
int a,b;
int (*ip)(int,int); //DECLARATION OF FUNCTION POINTER
int result;
printf("Enter the values of a and b : ");
scanf("%d %d",&a,&b);
ip=add;
result=(*ip)(a,b); //FUNCTION CALLING
printf("Value after addition is : %d",result);
return 0;
}
int add(int a,int b) // FUNCTION DEFINITION
{
int c=a+b;
return c;
}
Advantages of Pointers in C
• Pointers are useful for accessing memory locations.
• Pointers provide an efficient way for accessing the elements
of an array structure.
• Pointers are used for dynamic memory allocation as well as
deallocation.
• Pointers are used to form complex data structures such as
linked list, graph, tree, etc.
Disadvantages of Pointers in C
• Pointers are a little complex to understand.
• Pointers can lead to various errors such as segmentation faults or
can access a memory location which is not required at all.
• If an incorrect value is provided to a pointer, it may cause memory
corruption.
• Pointers are also responsible for memory leakage.
• Pointers are comparatively slower than that of the variables.
• Programmers find it very difficult to work with the pointers;
therefore it is programmer’s responsibility to manipulate a pointer
carefully.

You might also like