UNIT IV POINTERS & STRINGS
Syllabus: Introduction to Pointers, dereferencing and address operators, pointer and address
arithmetic, array manipulation using pointers. Introduction to Strings: String Operations and
String functions.
Introduction to pointers
Consider the below declaration:
int i = 3;
The above declaration tells the C compiler for:
1. Reserve space in memory to hold the integer value
2. Assign a name ‘i’ with this memory location. This is called as a variable
3. After that, store the value ‘3’ at this location
The representation of above 3 steps in memory by the memory map is shown given below:
Example 1: To print the address of a variableSource code:
Output:
,Definition of pointer:
The pointer 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 4 bytes.
Syntax and examples of pointers in C
data_type *pointer_variable_name;
Examples:
1. int *ptr_in; // pointer to an integer
2. char *ptr_ch ; // pointer to a character
3. double *ptr_dbl; // pointer to a double
4. float *ptr_fl; // pointer to a float
How to Use Pointers in C
1. Declare a pointer variable.
2. A variable’s address is assigned to a pointer variable using the & operator.
3. Use the address in the pointer variable to get the value by using the *(asterisk) operator,
which returns the variable's value at the address indicated by its argument.
Example 2: A program to create pointer variable
Output:
,Extended Pointers
Output:
Finding Size of pointer
The declaration char *ch means that ‘ch’ is going to contain the address of a char value.
Similarly, float *f means that ‘f’ going to contain the address of floating-point value.
The printf() would report the size of each pointer as 2Bytes, 4Bytes or 8Bytes based on
computer architecture 16-bit, 32-bit or 64-bit.
Note: This program I am executing on my machine is “64-bit operating system, x64-based
processor”. Therefore, the size is 8Bytes
, Source code:
#include<stdio.h>
void main()
{
int *ptr1;
char *ptr2;
float *ptr3;
double *ptr4;
printf("The size of integer pointer is %i", sizeof(ptr1));
printf("\nThe size of character pointer is %i", sizeof(ptr2));
printf("\nThe size of float pointer is %i", sizeof(ptr3));
printf("\nThe size of double pointer is %i", sizeof(ptr4));
}
Output:
Types of pointers
These are the following types of pointers:
1. NULL pointer
2. void pointer
3. Wild pointer
4. Dangling pointer
NULL Pointer
A null pointer is a type of pointer created by assigning a null value to the pointer. A null
pointer can be of any data type.
It has a value of 0 in it.
It indicates that the pointer is not intended to point to an accessible memory location.
Dereferencing null pointer results in undefined behavior, i.e., if you try to access as *ptr
(where ptr is a NULL pointer), it will result in a null pointer exception.
Example 6: A program to create a NULL pointer
Source code:
#include<stdio.h>
void main()
{
int *ptr1 = NULL;
printf("The value of ptr1 variable is %d", ptr1);
printf("\nThe address of ptr1 variable is %x", &ptr1);
printf("The dereferencing of ptr1 variable is %d", *ptr1);
}
Syllabus: Introduction to Pointers, dereferencing and address operators, pointer and address
arithmetic, array manipulation using pointers. Introduction to Strings: String Operations and
String functions.
Introduction to pointers
Consider the below declaration:
int i = 3;
The above declaration tells the C compiler for:
1. Reserve space in memory to hold the integer value
2. Assign a name ‘i’ with this memory location. This is called as a variable
3. After that, store the value ‘3’ at this location
The representation of above 3 steps in memory by the memory map is shown given below:
Example 1: To print the address of a variableSource code:
Output:
,Definition of pointer:
The pointer 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 4 bytes.
Syntax and examples of pointers in C
data_type *pointer_variable_name;
Examples:
1. int *ptr_in; // pointer to an integer
2. char *ptr_ch ; // pointer to a character
3. double *ptr_dbl; // pointer to a double
4. float *ptr_fl; // pointer to a float
How to Use Pointers in C
1. Declare a pointer variable.
2. A variable’s address is assigned to a pointer variable using the & operator.
3. Use the address in the pointer variable to get the value by using the *(asterisk) operator,
which returns the variable's value at the address indicated by its argument.
Example 2: A program to create pointer variable
Output:
,Extended Pointers
Output:
Finding Size of pointer
The declaration char *ch means that ‘ch’ is going to contain the address of a char value.
Similarly, float *f means that ‘f’ going to contain the address of floating-point value.
The printf() would report the size of each pointer as 2Bytes, 4Bytes or 8Bytes based on
computer architecture 16-bit, 32-bit or 64-bit.
Note: This program I am executing on my machine is “64-bit operating system, x64-based
processor”. Therefore, the size is 8Bytes
, Source code:
#include<stdio.h>
void main()
{
int *ptr1;
char *ptr2;
float *ptr3;
double *ptr4;
printf("The size of integer pointer is %i", sizeof(ptr1));
printf("\nThe size of character pointer is %i", sizeof(ptr2));
printf("\nThe size of float pointer is %i", sizeof(ptr3));
printf("\nThe size of double pointer is %i", sizeof(ptr4));
}
Output:
Types of pointers
These are the following types of pointers:
1. NULL pointer
2. void pointer
3. Wild pointer
4. Dangling pointer
NULL Pointer
A null pointer is a type of pointer created by assigning a null value to the pointer. A null
pointer can be of any data type.
It has a value of 0 in it.
It indicates that the pointer is not intended to point to an accessible memory location.
Dereferencing null pointer results in undefined behavior, i.e., if you try to access as *ptr
(where ptr is a NULL pointer), it will result in a null pointer exception.
Example 6: A program to create a NULL pointer
Source code:
#include<stdio.h>
void main()
{
int *ptr1 = NULL;
printf("The value of ptr1 variable is %d", ptr1);
printf("\nThe address of ptr1 variable is %x", &ptr1);
printf("The dereferencing of ptr1 variable is %d", *ptr1);
}