0% found this document useful (0 votes)
9 views10 pages

Pointer, Array

The document provides an overview of pointers in C, including their definition, types, and usage in relation to variables, arrays, and functions. It covers key concepts such as pointer arithmetic, dynamic memory allocation, and the differences between various pointer types like null, wild, and dangling pointers. Additionally, it includes code examples demonstrating how to declare, assign, and manipulate pointers effectively.

Uploaded by

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

Pointer, Array

The document provides an overview of pointers in C, including their definition, types, and usage in relation to variables, arrays, and functions. It covers key concepts such as pointer arithmetic, dynamic memory allocation, and the differences between various pointer types like null, wild, and dangling pointers. Additionally, it includes code examples demonstrating how to declare, assign, and manipulate pointers effectively.

Uploaded by

vidisha76a
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. What is a Pointer?

If you have a variable var in your program, &var will give you its address in the memory.
We have used address numerous times while using the scanf() function.

scanf("%d", &var);
Here, the value entered by the user is stored in the address of var variable. Let's take a
working example.
#include <stdio.h>
int main()
{
int var = 5;
printf("var: %d\n", var);

// Notice the use of & before var


printf("address of var: %p", &var);
return 0;
}

A pointer is a variable that stores the address of another variable.

Pointer Syntax

Here is how we can declare pointers. int* p;

Assigning addresses to Pointers


int* pc, c;
c = 5;
pc = &c;
Example:
#include <stdio.h>

int main() {
int a = 10;
int *p; // pointer to int
p = &a; // assign address of a to p

printf("Value of a: %d\n", a);


printf("Address of a: %p\n", p);
printf("Value pointed by p: %d\n", *p);
return 0;
}

📌 2. Types of Pointers in C
✅ a. Null Pointer

Points to nothing. Used for error handling.

int *p = NULL;

✅ b. Wild Pointer

Uninitialized pointer. Dangerous — must be avoided.

int *p; // wild pointer

✅ c. Dangling Pointer

Pointer pointing to a memory that has been freed or out of scope.

✅ d. Void Pointer

Generic pointer, can point to any data type.

void *ptr;
int a = 5;
ptr = &a;

To use the value, typecast is required:

printf("%d\n", *(int*)ptr);

✅ e. Constant Pointer vs Pointer to Constant


int a = 10, b = 20;
const int *p1 = &a; // pointer to constant (can't change *p1)
int *const p2 = &a; // constant pointer (can't point to another variable)

📌 3. Array and Pointer Relationship


In C, array names are pointers to the first element.

int arr[3] = {10, 20, 30};


int *p = arr; // same as &arr[0]

printf("%d\n", *p); // 10
printf("%d\n", *(p+1)); // 20

2. Pointer and Arrays


In this program, we use a pointer to iterate through an array.

#include <stdio.h>

int main() {
int arr[] = {1, 2, 3, 4, 5};
int *ptr = arr; // Pointer points to the first element of the array

printf("Array elements using pointer:\n");

for (int i = 0; i < 5; i++) {


printf("arr[%d] = %d\n", i, *(ptr + i)); // Dereference the pointer
to access each element
}

return 0;
}

Output:

Array elements using pointer:


arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5

3. Pointer to Pointer (Double Pointer)

This program demonstrates how a pointer to a pointer works in C.

#include <stdio.h>

int main() {
int num = 100;
int *ptr = &num; // Pointer pointing to the variable num
int **ptr2 = &ptr; // Pointer to the pointer ptr

printf("Value of num: %d\n", num); // Print the value of num


printf("Value using ptr: %d\n", *ptr); // Dereference ptr to get num
printf("Value using ptr2: %d\n", **ptr2); // Dereference ptr2 to get num

return 0;
}

Output:

Value of num: 100


Value using ptr: 100
Value using ptr2: 100

4. Pointer to Function
This program demonstrates how a pointer can be used to point to a function and call it.

#include <stdio.h>

// Function to be pointed to
void greet() {
printf("Hello from the function!\n");
}

int main() {
void (*func_ptr)(); // Declare a pointer to function

func_ptr = greet; // Assign address of greet function to func_ptr


func_ptr(); // Call greet function using the pointer

return 0;
}

Output:

Hello from the function!

5. Dynamic Memory Allocation Using Pointers

This example demonstrates how to allocate memory dynamically using malloc and then
deallocate it using free.

#include <stdio.h>
#include <stdlib.h>

int main() {
int *ptr;
int n = 5;

// Dynamically allocate memory for 5 integers


ptr = (int*)malloc(n * sizeof(int));

if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1;
}

// Initialize and print the allocated memory


for (int i = 0; i < n; i++) {
ptr[i] = i * 10; // Assign values to the dynamically allocated memory
printf("ptr[%d] = %d\n", i, ptr[i]);
}

// Free the allocated memory


free(ptr);

return 0;
}
Output:

ptr[0] = 0
ptr[1] = 10
ptr[2] = 20
ptr[3] = 30
ptr[4] = 40

Key Concepts:

 Pointers: Pointers store the memory address of variables.


 Dereferencing: The * operator is used to access the value at the memory address stored
in the pointer.
 Pointer to Pointer: A pointer can store the address of another pointer.
 Function Pointers: Pointers can also point to functions, allowing dynamic function calls.
 Dynamic Memory Allocation: Memory can be dynamically allocated at runtime using
functions like malloc or calloc and should be freed using free to avoid memory leaks.

1. Increment in a Pointer

In this program, we demonstrate how to increment a pointer to move to the next element in
an array.

#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer pointing to the first element of the array

printf("Original value pointed by ptr: %d\n", *ptr);

// Increment the pointer to point to the next element


ptr++;
printf("Value after incrementing ptr: %d\n", *ptr); // Now it points
to arr[1]

return 0;
}

Output:

Original value pointed by ptr: 10


Value after incrementing ptr: 20

2. Decrement in a Pointer
This program demonstrates how to decrement a pointer to move to the previous element in an
array.

#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = &arr[4]; // Pointer initially pointing to the last element
(arr[4])

printf("Original value pointed by ptr: %d\n", *ptr);

// Decrement the pointer to point to the previous element


ptr--;
printf("Value after decrementing ptr: %d\n", *ptr); // Now it points
to arr[3]

return 0;
}

Output:

Original value pointed by ptr: 50


Value after decrementing ptr: 40

3. Addition of Integer to a Pointer

This program demonstrates how to add an integer to a pointer, which moves the pointer
forward by that number of elements in memory.

#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = arr; // Pointer to the first element

printf("Original value pointed by ptr: %d\n", *ptr);

// Add an integer (2) to the pointer to move it 2 positions forward


ptr = ptr + 2;
printf("Value after adding 2 to the pointer: %d\n", *ptr); // Now it
points to arr[2]

return 0;
}

Output:

Original value pointed by ptr: 10


Value after adding 2 to the pointer: 30
4. Subtraction of Integer from a Pointer

This program demonstrates how to subtract an integer from a pointer, which moves the
pointer backward by that number of elements.

#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr = &arr[4]; // Pointer pointing to the last element (arr[4])

printf("Original value pointed by ptr: %d\n", *ptr);

// Subtract an integer (2) from the pointer to move it 2 positions


backward
ptr = ptr - 2;
printf("Value after subtracting 2 from the pointer: %d\n", *ptr); //
Now it points to arr[2]

return 0;
}

Output:

Original value pointed by ptr: 50


Value after subtracting 2 from the pointer: 30

5. Subtracting Two Pointers of the Same Type

This program demonstrates how to subtract two pointers that point to elements of the same
array. The result is the number of elements between them.

#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr1 = arr; // Pointer to the first element
int *ptr2 = &arr[4]; // Pointer to the last element

// Subtracting the two pointers gives the number of elements between


them
int diff = ptr2 - ptr1;

printf("Difference between two pointers: %d\n", diff); // Should print


4 (from arr[0] to arr[4])

return 0;
}

Output:
Difference between two pointers: 4

6. Comparison of Pointers of the Same Type

This program demonstrates how to compare two pointers that point to elements of the same
array.

#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr1 = &arr[1]; // Pointer to arr[1]
int *ptr2 = &arr[2]; // Pointer to arr[2]

if (ptr1 < ptr2) {


printf("ptr1 points to a smaller address than ptr2\n");
} else {
printf("ptr1 does not point to a smaller address than ptr2\n");
}

return 0;
}

Output:

ptr1 points to a smaller address than ptr2

7. Assignment of Pointers of the Same Type

This program demonstrates how one pointer can be assigned to another pointer of the same
type.

#include <stdio.h>

int main() {
int arr[] = {10, 20, 30, 40, 50};
int *ptr1 = arr; // Pointer to the first element
int *ptr2; // Another pointer

// Assigning ptr1 to ptr2


ptr2 = ptr1;

// Both pointers now point to the same location


printf("Value pointed by ptr1: %d\n", *ptr1);
printf("Value pointed by ptr2: %d\n", *ptr2);

return 0;
}

Output:
Value pointed by ptr1: 10
Value pointed by ptr2: 10

📌 6. Dynamic Memory Allocation Using Pointers


✅ malloc() – Allocates memory
int *ptr = (int *)malloc(5 * sizeof(int));

// ✅ malloc(5 * sizeof(int))

 malloc stands for memory allocation.


 It allocates a block of memory of the size given in bytes.
 5 * sizeof(int) tells malloc to allocate space for 5 integers:
o sizeof(int) returns the size of an integer on your system (typically 4 bytes).
o 5 * sizeof(int) = 20 bytes (on most systems).

✅ (int *)

 malloc returns a void * (generic pointer).


 We typecast it to int *, since we're storing integer values.

✅ int *ptr = ...

 ptr is a pointer to integer.


 It now points to the first element of the dynamically allocated memory block.

✅ calloc() – Allocates and initializes to zero


int *ptr = (int *)calloc(5, sizeof(int));

✅ realloc() – Resize memory block


ptr = (int *)realloc(ptr, 10 * sizeof(int));

✅ free() – Deallocates memory


free(ptr);

Example:
#include <stdio.h>
#include <stdlib.h>

int main() {
int *arr;
arr = (int *)malloc(3 * sizeof(int));

arr[0] = 10; arr[1] = 20; arr[2] = 30;

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


printf("%d ", *(arr + i));

free(arr);
return 0;
}

You might also like