C lecture 5.1
C lecture 5.1
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'
#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;
}