0% found this document useful (0 votes)
2 views

C lecture 5.1

The document provides an overview of pointers in C, explaining their definition, declaration, and usage, including NULL and generic pointers. It covers pointer arithmetic, passing pointers to functions, and the relationship between arrays and pointers. Examples are provided to illustrate these concepts and their practical applications in C programming.

Uploaded by

devlina.karmakar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

C lecture 5.1

The document provides an overview of pointers in C, explaining their definition, declaration, and usage, including NULL and generic pointers. It covers pointer arithmetic, passing pointers to functions, and the relationship between arrays and pointers. Examples are provided to illustrate these concepts and their practical applications in C programming.

Uploaded by

devlina.karmakar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

C Pointers:

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 = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.

Declaration of Pointer:
The pointer in c language can be declared using * (asterisk symbol). It is also known as indirection pointer used to
dereference a pointer.
int *a;//pointer to int
char *c;//pointer to char
Accessing of Pointer:

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.
Example:

#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 therefore printing p gives the address of n
umber.
printf("Value of p variable is %d \n",*p); // As we know that * is used to dereference a pointer therefore if we print *p,
we will get the value stored at the address contained by p.
return 0;
}
NULL Pointer:
A pointer that is not assigned any value but NULL is known as the NULL pointer. If we don't have any address to be
specified in the pointer at the time of declaration, we can assign NULL value. It will provide a better approach.
A Null Pointer is a pointer that does not point to any memory location. It stores the base address of the segment. The
null pointer basically stores the Null value while void is the type of the pointer.
• A null pointer is a special reserved value which is defined in a stddef header file. Here, Null means that the pointer
is referring to the 0th memory location.
• If we do not have any address which is to be assigned to the pointer, then it is known as a null pointer. When a
NULL value is assigned to the pointer, then it is considered as a Null pointer

Syntax:
int *ptr=NULL;
char *ptr='\0';
Example of NULL Pointer:
#include <stdio.h>
int main()
{
int *ptr=NULL;
if(ptr!=NULL)
{
printf("value of ptr is : %d",*ptr);
}
else
{
printf("Invalid pointer");
}
return 0;
}
Generic Pointer:
A generic pointer is a pointer that has no associated data type with it. A generic pointer can hold address of any type and can
be typcasted to any type. It is also called void pointer.
Syntax:
int a = 10;
char b = 'x';
void *p = &a; // void pointer holds address of int 'a'
p = &b; // void pointer holds address of char 'b'

Advantages of void pointers:


malloc() and calloc() return void * type and this allows these functions to be used to allocate memory of any data type (just
because of void *)
void pointers in C are used to implement generic functions in C.
#include<stdio.h>
int main()
{
int a=10;
float b=35.75;
void *ptr; // Declaring a void pointer
ptr=&a; // Assigning address of integer to void pointer.
printf("The value of integer variable is= %d\n",*( (int*) ptr) ); // (int*)ptr - is used for type casting. Where as *((int*)ptr)
dereferences the typecasted void pointer variable.
ptr=&b; // Assigning address of float to void pointer.
printf("The value of float variable is= %f\n",*( (float*) ptr) );
return 0;
}
Pointer Expression and Arithmetic :
Like other variables pointer variables can be used in expressions.
If p1 and p2 are properly declared and initialized pointers, then the following statements are valid:
Y=*p1**p2;
Sum=sum+*p1;
Z=5*-*p2/ *p1;
*p2=*p2+10;
*p1=*p1+*p2;
*p1=*p2-*p1;
NOTE: in the third statement there is a blank space between ‘/’ and * because the symbol /*is considered as beginning of the
comment and therefore the statement fails.
if p1 and p2 are properly declared and initialized pointers then, ‘C’ allows adding integers to a pointer variable.
int a=5, b=10;
int *p1,*p2;
p1=&a;
p2=&b;
If p1 & p2 are properly declared and initialize pointers, and both points to the elements of same type. “Subtraction of one
pointer from another pointer is also possible".
Passing arguments to function using pointers:
When we pass a pointer as an argument instead of a variable then the address of the variable is passed instead of the value. So any change made by the function
using the pointer is permanently made at the address of passed variable. This technique is known as call by reference in C.
#include <stdio.h>
void swapnum(int *num1, int *num2)
{
int tempnum;
tempnum = *num1;
*num1 = *num2;
*num2 = tempnum;
}
void main( )
{
int v1 = 11, v2 = 77 ;
printf("Before swapping:");
printf("\nValue of v1 is: %d", v1);
printf("\nValue of v2 is: %d", v2); /*calling swap function*/
swapnum( &v1, &v2 );
printf("\nAfter swapping:");
printf("\nValue of v1 is: %d", v1);
printf("\nValue of v2 is: %d", v2);
}
Pointers and Array:

#include <stdio.h>
int main()
{
int i, x[6], sum = 0;
printf("Enter 6 numbers: ");
for(i = 0; i < 6; ++i) // Equivalent to scanf("%d", &x[i])
{
scanf("%d", x+i); // Equivalent to sum += x[i]
sum=sum+*(x+i);
}
printf("Sum = %d", sum);
return 0;
}
Passing an array to a function(using call by value method):

#include <stdio.h>
void disp( char ch)
{
printf("%c ", ch);
}
int main()
{
char arr[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
for (int x=0; x<10; x++)
{
disp (arr[x]);
}
return 0;
}
Passing an array to a function(using call by reference method):

#include <stdio.h>
void disp( int *num)
{
printf("%d ", *num);
}
int main()
{
int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
for (int i=0; i<10; i++)
{
disp (&arr[i]);
}
return 0;
}
Arrays and Pointers:
The difference between a pointer variable and an array name is that you can never change the address of the array name. It will always
point to the first element of the array as long as it exists. The pointer variable on the other hand are quite flexible and can be pointed to
somewhere else during the course of the program.

#include <stdio.h>
int main()
{
int a[4];
int i;
for(i = 0; i < 4; ++i)
{
printf("&a[%d] = %x\n", i, &a[i]);
}
printf("Address of array x: %x", a);
return 0;
}

You might also like