0% found this document useful (0 votes)
1 views7 pages

Module 1

The document provides an overview of arrays, pointers, structures, and unions in C programming. It covers the declaration, initialization, and manipulation of one-dimensional and multi-dimensional arrays, as well as dynamic memory allocation techniques. Additionally, it explains pointers, their arithmetic, and applications, along with structures and unions for organizing data of different types.

Uploaded by

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

Module 1

The document provides an overview of arrays, pointers, structures, and unions in C programming. It covers the declaration, initialization, and manipulation of one-dimensional and multi-dimensional arrays, as well as dynamic memory allocation techniques. Additionally, it explains pointers, their arithmetic, and applications, along with structures and unions for organizing data of different types.

Uploaded by

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

Module 1: Arrays, Pointers, Structures and Unions

1. Arrays

1.1 Introduction

An array is a finite ordered collection of homogeneous data elements, stored in contiguous memory
locations. Arrays allow efficient element access through indexing and are suitable for storing large
datasets where random access is required.

1.2 One-Dimensional Arrays

A one-dimensional array is a list of elements of the same type accessed using a single index.

Declaration:

int marks[5];

Initialization:

int marks[5] = {45, 67, 89, 32, 75};

Accessing and Modifying Elements:

marks[2] = 90;

printf("%d", marks[2]); // Output: 90

Traversal Example:

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

printf("%d\n", marks[i]);

1.3 Two-Dimensional Arrays

A 2D array is an array of arrays, commonly used for matrix operations.

Declaration:

int mat[3][4];

Initialization:

int mat[2][2] = {{1, 2}, {3, 4}};

Row-Major Memory Layout:


Stored sequentially as: mat[0][0], mat[0][1], mat[1][0], mat[1][1].

Address Formula (Row-Major):

Loc(mat[i][j]) = Base + w * (i * no_of_cols + j)

Where w is the size (in bytes) of a single array element.

1.4 Multidimensional Arrays


Higher-dimensional arrays are declared as:

int a[2][3][4];

These are typically used in numerical simulations and tensor calculations.

1.5 Dynamic Memory Allocation for Arrays

In C, dynamic arrays are allocated using functions from stdlib.h.

1D Dynamic Array:

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

2D Dynamic Array:

int **arr = (int **)malloc(rows * sizeof(int*));

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

arr[i] = (int *)malloc(cols * sizeof(int));

Deallocation:

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

free(arr[i]);

free(arr);

2. Pointers

2.1 Introduction

A pointer is a variable that holds the address of another variable. Pointers enhance memory efficiency
and enable powerful features like dynamic memory allocation, function pointers, and linked data
structures.

Declaration and Initialization:

int x = 10;

int *p = &x;

Here, p holds the address of x, and *p accesses the value.

Example:

printf("%d", *p); // Output: 10

2.2 Pointer Arithmetic

Pointers can be incremented/decremented using arithmetic operations:

p = p + 1; // Moves to next memory location of type pointed

In arrays:
*(arr + i) == arr[i]

2.3 Applications of Pointers

• Access array elements efficiently

• Pass large structures to functions by reference

• Implement data structures (linked lists, trees)

• Allocate memory dynamically

2.4 Dynamic Memory Allocation Functions

1. malloc(size_t size)

• Allocates a single block of memory of specified size.

• Memory is uninitialized; may contain garbage values.

• Returns a void pointer, which must be typecasted.

Example:

int *arr = (int *)malloc(5 * sizeof(int));

#include <stdio.h>

#include <stdlib.h>

int main() {

int *arr;

arr = (int *)malloc(5 * sizeof(int));

if (arr == NULL) {

printf("Memory not allocated.\n");

return 1;

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

arr[i] = i + 1;

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

printf("%d ", arr[i]);


free(arr);

return 0;

2. calloc(size_t n, size_t size)

• Allocates memory for an array of n elements of size bytes each.

• Initializes all bytes to zero.

Example:

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

#include <stdio.h>

#include <stdlib.h>

int main() {

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

if (arr == NULL) {

printf("Memory not allocated.\n");

return 1;

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

printf("%d ", arr[i]); // All will be 0

free(arr);

return 0;

3. realloc(void *ptr, size_t new_size)

• Resizes the memory block pointed to by ptr to new_size bytes.

• Useful when you want to expand or shrink previously allocated memory.

Example:

#include <stdio.h>

#include <stdlib.h>
int main() {

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

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

arr[i] = i + 1;

arr = (int *)realloc(arr, 5 * sizeof(int));

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

arr[i] = i + 1;

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

printf("%d ", arr[i]);

free(arr);

return 0;

4. free(void *ptr)

• Frees the memory space pointed to by ptr, which must have been returned by one of the
memory allocation functions.

• After using free, the pointer becomes a dangling pointer and should be set to NULL.

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

free(ptr);

ptr = NULL;

2.5 Dangling and Wild Pointers

• Dangling Pointer: A pointer that refers to a memory location that has already been freed.

• Wild Pointer: A pointer that has not been initialized.

int *p; // wild pointer

*p = 10; // undefined behavior

int *q = (int*)malloc(sizeof(int));

free(q);
*q = 20; // dangling pointer

3. Structures and Unions

3.1 Structures

Structures allow combining data of different types under a single unit.

typedef struct {

int id;

char name[30];

float salary;

} Employee;

Employee e1 = {1001, "Anil", 50000};

3.2 Nested Structures

typedef struct {

int day, month, year;

} Date;

typedef struct {

char name[20];

Date dob;

} Person;

Access:

Person p;

p.dob.day = 15;

3.3 Array of Structures

Employee emp[5];

3.4 Comparison of Structure Variables

Manual comparison is required.

if (e1.id == e2.id && strcmp(e1.name, e2.name) == 0) {...}

3.5 Unions

A union is similar to a structure, but all members share the same memory.

union Data {
int i;

float f;

char str[20];

};

Usage:

union Data d;

d.i = 10;

printf("%d", d.i);

You might also like