Data Structures PDF - Arrays, Pointers, Functions - Jain University

Summary

This document is a set of lecture slides on Data Structures, covering topics such as arrays, pointers, and functions in the C programming language. The material includes definitions, syntax, and examples. It originates from Jain University, Kochi.

Full Transcript

DATA STRUCTURES Arrays: Array initialization: Static and Dynamic,bound checking 2d Array,initialization of 2D array. Pointers: Representation, Initialization, Operators. Function: What is function,passing arguments to function,passing arguments by value, returning value,passing arguments by ref...

DATA STRUCTURES Arrays: Array initialization: Static and Dynamic,bound checking 2d Array,initialization of 2D array. Pointers: Representation, Initialization, Operators. Function: What is function,passing arguments to function,passing arguments by value, returning value,passing arguments by reference, scope rule of function, Recursion, storage classes and scope, Library functions like math library,recursion. Lab: Programs On Arrays,Pointers and Functions. Introduction to Arrays What is an Array? An array is a collection of items stored at contiguous memory locations. - Example: int numbers; - Allows storing multiple values of the same type. Static Array Initialization Definition: Static Array Initialization refers to the process of defining and initializing an array at compile time. Syntax: data_type array_name[array_size] = {value1, value2,..., valueN}; Eg :char letters = {'A', 'B', 'C', 'D'}; Dynamic Array Initialization Definition: Dynamic Array Initialization involves creating and initializing arrays at runtime, allowing flexibility in size and memory allocation. malloc: Allocates uninitialized memory. calloc: Allocates and initializes memory to zero. Remember to free allocated memory using free(array_name) to avoid memory leaks. Syntax: data_type *array_name = (data_type *)malloc/calloc(array_size * sizeof(data_type)); Bound checking in arrays Definition: Bound checking refers to the process of ensuring that array indices are within the defined range during access. Risks of Accessing Out-of-Bounds Elements: Accessing array elements outside the defined range leads to unpredictable results. Accessing beyond array size: int arr; arr = 20; // Undefined behavior Using negative indices: arr[-1] = 30; // Undefined behavior 2D array: Static Initialization 2D array: Accessing and manipulation Pointers A pointer can be used to store the memory address of other variables, functions, or even other pointers. Syntax of C Pointers The syntax of pointers is similar to the variable declaration in C, but we use the ( * ) dereferencing operator in the pointer declaration. datatype * ptr; where ptr is the name of the pointer. datatype is the type of data it is pointing to. The use of pointers in C can be divided into three steps: 1. Pointer Declaration 2. Pointer Initialization 3. Pointer Dereferencing Pointer Declaration To declare a pointer, we use the ( * ) dereference operator before its name. Example int *ptr; The pointer declared here will point to some random memory address as it is not initialized. Such pointers are called wild pointers. Pointer Initialization Pointer initialization is the process where we assign some initial value to the pointer variable. We generally use the ( &: ampersand ) addressof operator to get the memory address of a variable and then store it in the pointer variable. Example int var = 10; int * ptr; ptr = &var; Pointer Dereferencing Dereferencing a pointer is the process of accessing the value stored in the memory address specified in the pointer. int var = 10; int * ptr; ptr = &var;