0% found this document useful (0 votes)
62 views23 pages

Pointers Types

Pointer arithmetic allows accessing elements of an array using pointers. An array name acts as a pointer to the first element of the array. Incrementing a pointer increases its address by the size of the data type it points to. Pointers of the same type can be subtracted to get the number of elements between them. The size of pointers depends on the system architecture and is usually 4 bytes for 32-bit and 8 bytes for 64-bit systems.

Uploaded by

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

Pointers Types

Pointer arithmetic allows accessing elements of an array using pointers. An array name acts as a pointer to the first element of the array. Incrementing a pointer increases its address by the size of the data type it points to. Pointers of the same type can be subtracted to get the number of elements between them. The size of pointers depends on the system architecture and is usually 4 bytes for 32-bit and 8 bytes for 64-bit systems.

Uploaded by

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

POINTERS

C Pointers and Arrays

• An array name acts like a pointer constant.


• The value of this pointer constant is the
address of the first element.
• For example, if we have an array named val
then val and &val[0] can be used
interchangeably.
Accessing Array Elements using Pointer with Array Subscript
// C Program to access array elements using pointer
#include <stdio.h>
Void ptr_arr()
{
// Declare an array
int val[3] = { 5, 10, 15 };
// Declare pointer variable
int* ptr;
// Assign address of val[0] to ptr.
// We can use ptr=&val[0];(both are same)
ptr = val;
printf("Elements of the array are: ");
printf("%d, %d, %d", ptr[0], ptr[1], ptr[2]);
return;
}

int main()
{
ptr_arr();
return 0;
}
Accessing Array Elements using Pointer Arithmetic
// C Program to access array elements using pointers
#include <stdio.h>

int main()
{

// defining array
int arr[5] = { 1, 2, 3, 4, 5 };

// defining the pointer to array


int* ptr_arr = &arr;

// traversing array using pointer arithmetic


for (int i = 0; i < 5; i++) {
printf("%d ", *ptr_arr++);
}
return 0;
}
Increment/Decrement of a Pointer
• Increment: When a pointer is incremented, it actually increments by the
number equal to the size of the data type for which it is a pointer.
• For Example:
If an integer pointer that stores address 1000 is incremented, then it will
increment by 4(size of an int), and the new address will point to 1004.
• While if a float type pointer is incremented then it will increment by 4(size
of a float) and the new address will be 1004.
Addition of Integer to Pointer
• When a pointer is added with an integer value, the value is
first multiplied by the size of the data type and then added to
the pointer.
• If the ptr is an integer pointer that stores 1000 as an address.
• If we add integer 5 to it using the expression, ptr = ptr +
5, then, the final address stored in the ptr will be ptr = 1000 +
sizeof(int) * 5 = 1020.
Subtraction of Integer to Pointer
Subtraction of Two Pointers

• The subtraction of two pointers is possible only when they


have the same data type.
• The result is generated by calculating the difference between
the addresses of the two pointers and calculating how many
bits of data it is according to the pointer data type.
• The subtraction of two pointers gives the increments
between the two pointers.
• For Example:
Two integer pointers say
ptr1(address:1000) and ptr2(address:1004) are subtracted.
• The difference between addresses is 4 bytes. Since the size
of int is 4 bytes, therefore the increment between ptr1 and
ptr2 is given by (4/4) = 1.
Comparison of Pointers

• The two pointers can be compared by using all


operators in C >, >=, <, <=, ==, !=.
• It returns true for the valid condition and
returns false for the unsatisfied condition.
• Step 1: Initialize the integer values and point
these integer values to the pointer.
• Step 2: Now, check the condition by using
comparison or relational operators on pointer
variables.
• Step 3: Display the output.
Size of Pointers in C
• The size of the pointers in C is equal for every pointer type.
• The size of the pointer does not depend on the type it is
pointing to.
• It only depends on the operating system and CPU architecture.
• The size of pointers in C is
– 8 bytes for a 64-bit System
– 4 bytes for a 32-bit System
• The reason for the same size is that the pointers store the
memory addresses, no matter what type they are.
• As the space required to store the addresses of the different
memory locations is the same, the memory required by one
pointer type will be equal to the memory required by other
pointer types.
// C Program to find the size of different pointers types
#include <stdio.h>
void func(int a, int b){};
int main(){
int a = 10;
char c = 'G';
// pointer definitions of different types
int* ptr_int = &a;
char* ptr_char = &c;
void (*ptr_func)(int, int) = &func;
void* ptr_vn = NULL;
// printing sizes
printf("Size of Integer Pointer \t:\t%d bytes\n",
sizeof(ptr_int));
printf("Size of Character Pointer\t:\t%d bytes\n",
sizeof(ptr_char));
printf("Size of Function Pointer\t:\t%d bytes\n",
sizeof(ptr_func));
printf("Size of NULL Void Pointer\t:\t%d bytes",
sizeof(ptr_vn));

return 0;
}
• // C program to illustrate Pointer Arithmetic
• #include <stdio.h>
• int main(){
• // Declare an array
• int v[3] = { 10, 100, 200 };

• // Declare pointer variable


• int* ptr;

• // Assign the address of v[0] to ptr


• ptr = v;

• for (int i = 0; i < 3; i++) {

• // print value at address which is stored in ptr


• printf("Value of *ptr = %d\n", *ptr);

• // print value of ptr


• printf("Value of ptr = %p\n\n", ptr);

• // Increment pointer ptr by 1


• ptr++;
• }
• return 0;
• }

You might also like