A ppt on lecture 5 programming in C language pointers
Size: 2.93 MB
Language: en
Added: May 18, 2025
Slides: 41 pages
Slide Content
UNIT-5 (POINTERS)
Pointer Pointers is a special variable that is capable to storing some address. The purpose of pointer is to save memory space and achieve faster execution time. It points to a memory location where first byte is stored. Pointers can be used with array and string to access elements more efficiently.
Variable versus Pointer VARIABLE POINTER A value stored in a named storage/memory address A variable that points to the storage/memory address of another variable
Initialize a pointer pointer = &variable; To get the address of a variable, we use the ampersand (&)operator, placed before the name of a variable whose address we need. Pointer initialization is done with the following syntax: Example:
Operator Meaning * Declaration of a pointer Returns the value of the referenced variable & 1. Returns the address of a variable To use pointers in C, we must understand below two operators. To access the value stored in the address we use the unary operator (*) that returns the value of the variable located at the address specified by its operand. This is also called Dereferencing .
Example #include < stdio.h > int main() { int Var = 10; int * ptr = &Var; // A pointer variable that holds address of var. printf ("Value of Var = %d\n", * ptr ); printf ("Address of Var = %d\n ", ptr ); * ptr = 20; // Value at address is now 20 printf ("After doing * ptr = 20, * ptr is %d\n", * ptr ); return 0; } Output : Value of Var = 10 Address of Var = 0x7fffa057dd4 After doing * ptr = 20, * ptr is 20
Applications of Pointer Pointers are useful for accessing memory locations. Pointers provide an efficient way for accessing the elements of an array structure. Pointers are used for dynamic memory allocation as well as deallocation. Pointers are used to form complex data structures such as linked list, graph, tree, etc.
Dynamic Memory Allocation The process of allocating memory at the time of execution is called dynamic memory allocation. Dynamic Memory Allocation can also be defined as a procedure in which the size of a data structure (like Array) is changed during the runtime. Pointers play an important role in dynamic memory allocation. Allocated memory can only be accessed through pointers. There are 4 library functions provided by C defined under < stdlib.h > header file to facilitate dynamic memory allocation in C programming. They are: malloc() calloc () free() realloc ()
malloc() method “malloc” or “memory allocation” method in C is used to dynamically allocate a single large block of memory with the specified size. It returns a pointer of type void which can be cast into a pointer of any form. It initializes each block with default garbage value. Syntax: For Example : int * ptr ; ptr = (int*) malloc(100 * sizeof (int)); Since if the size of int is 2 bytes, this statement will allocate 200 bytes of memory. And, the pointer ptr holds the address of the first byte in the allocated memory. ptr var = ( data -type*) malloc (n* sizeof ( datatype ));
C calloc () method “ calloc ” or “contiguous allocation” method in C is used to dynamically allocate the specified number of blocks of memory of the specified type. It initializes each block with a default value ‘0’. Syntax: ptr var = (data-type *) calloc (n , sizeof ( datatype ) ); For Example : int * ptr ; ptr = (float *) calloc (25 , sizeof (float)); This statement allocates contiguous space in memory for 25 elements each with the size of the float(i.e. 100).
realloc () method “ realloc ” or “re-allocation” method in C is used to dynamically change the memory allocation of a previously allocated memory. In other words, if the memory previously allocated with the help of malloc or calloc is insufficient, realloc can be used to dynamically re-allocate memory . re-allocation of memory maintains the already present value and new blocks will be initialized with default garbage value. Syntax : int * ptr ; ptr = realloc ( ptr , newSize ); where ptr is reallocated with new size ' newSize '.
free() method “free” method in C is used to dynamically de-allocate the memory. The memory allocated using functions malloc() and calloc () is not de-allocated on their own. Hence the free() method is used, whenever the dynamic memory allocation takes place. It helps to reduce wastage of memory by freeing it. Syntax : int * ptr ; free( ptr );
malloc() allocates single block of requested memory. calloc() allocates multiple block of requested memory. realloc() reallocates the memory occupied by malloc() or calloc() functions. free() frees the dynamically allocated memory. L et's have a quick look at the methods used for dynamic memory allocation.
Difference Static memory allocation Dynamic memory allocation memory is allocated at compile time. memory is allocated at run time. memory can't be increased while executing program. memory can be increased while executing program. used in array. used in linked list.
Self-referential structure A structure can have members which point to a structure variable of the same type . These types of structures are called self referential structures and are widely used in dynamic data structures like trees, linked list, etc. The following is a definition of a self referential structure. struct node { int data; struct node *next ; }; Here , next is a pointer to a struct node variable. It should be remembered that a pointer to a structure is similar to a pointer to any other variable. A self referential data structure is essentially a structure definition which includes at least one member that is a pointer to the structure of its own kind.
In other words, structures pointing to the same type of structures are self-referential in nature.
#include < stdio.h > #include < conio.h > struct red { int data; float val ; struct red *link ; }; int main() { struct red object1; //link1 object1.link = NULL; object1.data = 10; object1.val = 20.5; struct ref object2; // object2.link = NULL; object2.data = 30; object2.val = 40.3; object1.link = &object2; printf (“%d \n”, object1.link -> data); printf (“%d \n”, object1.link -> val ); return 0; }
Types of Self Referential Structures Self Referential Structure with Single Link Self Referential Structure with Multiple Links
1. Self Referential Structure with Single Link: These structures can have only one self-pointer as their member. The following example will show us how to connect the objects of a self-referential structure with the single link and access the corresponding data members. The connection formed is shown in the following figure.
2. Self Referential Structure with Multiple Links: Self referential structures with multiple links can have more than one self-pointers. Many complicated data structures can be easily constructed using these structures. Such structures can easily connect to more than one nodes at a time. The following example shows one such structure with more than one links.
Applications Self referential structures are very useful in creation of other complex data structures like: Linked Lists Stacks Queues Trees Graphs etc
Linked List A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. Linked list is the second most used data structure after array. The elements in a linked list are linked using pointers as shown in the below image In simple words, a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list.