1 of 56

POINTERS

2 of 56

POINTERS

  • A Pointer in C language is a variable that holds a memory address. This memory address is the address of another variable of same data type.
  • In simple words, if one variable stores the address of second variable then the first variable can be said to point towards the second variable.

3 of 56

Address of a memory location

  • What is a Memory Address in C?
  • Whenever a variable is defined in C language, a memory location is assigned for it, in which it's value gets stored. We can check this memory address, using the & symbol.
  • If var is the name of the variable, then &var will give it's address.

4 of 56

What is a Pointer

  • A Pointer in C language is a variable that holds a memory address.
  • Pointers are used to access memory of a variable and manipulate the value stored in it.
  • Pointers are one of the most distinct and exciting features of C language.

5 of 56

What is a Pointer

  • It provides power and flexibility to the language.
  • Although pointers may appear a little confusing and complicated in the beginning, but once we understand the concept, we will be able to do so much more with C language.

6 of 56

What is a Pointer

  • Whenever a variable is declared in a program, system allocates a location i.e an address to that variable in the memory, to hold the assigned value.
  • This location has its own address number.

7 of 56

Pointer Operators in C�

  • Pointer Operators in C
  • There are two pointer operators in C, they are:
  • * operator
  • & operator

8 of 56

Pointer Operators in C�

  • The & operator returns the memory address of its operand.
  • a = &b;
  • In the variable a the memory address of the variable b will get stored.
  • The * operators is the complement of &. This operator returns the value located at the given address.
  • For example, if a contains the memory address of the variable b,
  • c = *a;
  • , will store the value of the variable b into c.

9 of 56

  • Pointers are more efficient in handling Arrays in C and Structures in C.
  • Pointers allow references to function and thereby helps in passing of function as arguments to other functions.
  • Pointers also provide means by which a function in C can change its calling arguments.
  • It reduces length of the program and its execution time as well.
  • It allows C language to support Dynamic Memory management.

10 of 56

  • We can implement linked lists, trees, and graphs using pointers.

11 of 56

Declaring a Pointer in C�

  • The general syntax of pointer declaration is,
  • datatype *pointer_name;
  • Here, pointer_name is the name of the pointer and that should be a valid C identifier.
  • The datatype of the pointer and the variable to which the pointer variable is pointing must be the same

12 of 56

Declaring a Pointer in C�

  • Following are some examples of declaring a pointer in C:
  • int *ptr; //pointer to int
  • float *ptr; //pointer to float
  • char *ptr; //pointer to char
  • double *ptr; //pointer to double

13 of 56

Assigning Value to a Pointer Variable�

  • When we declare a pointer, it contains garbage value, which means it could be pointing anywhere in the memory.
  •  Pointer Initialization is the process of assigning the address of a variable to a pointer. In C language, the address operator & is used to determine the address of a variable. The & (immediately preceding a variable name) returns the address of the variable associated with it.
  • For example,

14 of 56

15 of 56

Derefrencing Pointer in C�

  • Once a pointer has been assigned the address of a variable, the pointer is dereferenced, using the indirection operator or dereferencing operator, which is a *, to access the value of the variable.

16 of 56

Derefrencing Pointer in C�

  • int x = 10;
  • int *p = &x; // p stores the address of x
  • printf("%d", *p); // dereferencing p → prints the value at that address → 10
  • Here: p → contains the address of x
  • *p → dereferences the pointer and gives the value 10

17 of 56

Derefrencing Pointer in C�

  • A pointer stores an address
  • A dereferenced pointer gives the value at that address

18 of 56

a) To declare a pointer to a variable:

  • //Declare a pointer
  • data_type_name * variable name
  • //Example int *ptr;

19 of 56

b)Initialization of pointer variables:

  • The process of assigning the address of a variable to a pointer variable is known as initialization.
  • Once a pointer variable has been declared we can use assignment operator to initialize the variable.

20 of 56

Benefits of using pointers are:-

  • 1)Pointers are more efficient in handling arrays and data tables.
  • 2) Pointers can be used to return multiple values from a function via function arguments.
  • 3) The use of pointer arrays to character strings results in saving of data storage space in memory.
  • 4) Pointers allow C to support dynamic memory management.
  • 5) Pointers provide an efficient tool for manipulating dynamic data structures such as structures , linked lists , queues , stacks and trees.
  • 6) Pointers reduce length and complexity of programs.
  • 7) They increase the execution speed and thus reduce the program execution time

21 of 56

C Pointer Arithmetic�

  • C Pointer Arithmetic

The pointer arithmetic refers to the arithmetic operations that can be performed on a pointer. It is slightly different from the ones that we generally use for mathematical calculations as only a limited set of operations can be performed on pointers.

22 of 56

C Pointer Arithmetic

These operations include:

  • Increment/Decrement
  • Addition/Subtraction of Integer
  • Subtracting Two Pointers of Same Type
  • Comparison of pointers

23 of 56

Incrementing a Pointer:�

  • Incrementing/Decrementing a Pointer:
  • ptr++ or ++ptr:
  • Increases the pointer's address by the size of the data type it points to.
  • If ptr points to an int, ptr++ moves it forward by sizeof(int) bytes.

24 of 56

Incrementing a Pointer:�

  • For Example:�If an integer pointer that stores address 1000 is incremented, then it will increment by 2(size of an int), and the new address will point to 1002.
  • 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.

25 of 56

Decrementing a Pointer:�

  • ptr-- or --ptr:
  • Decreases the pointer's address by the size of the data type it points to.

26 of 56

Decrementing a Pointer:�

  • For Example:�If an integer pointer that stores address 1000 is decremented, then it will decrement by 2(size of an int), and the new address will point to 998.
  • While if a float type pointer is decremented then it will decrement by 4(size of a float) and the new address will be 996.

27 of 56

Adding an Integer to a Pointer:�

  • ptr + n:
  • Adds n * sizeof(data_type) bytes

to the pointer's address.

  • The result is a pointer to the element n positions after the original.

28 of 56

Subtracting an Integer from a Pointer:

  • ptr - n:
  • Subtracts n * sizeof(data_type) bytes

from the pointer's address.

  • The result is a pointer to the element n positions before the original.

29 of 56

Subtracting Two Pointers:�

  • Subtracting Two Pointers:
  • ptr1 - ptr2:
  • Returns an integer value representing the number of elements between the two pointers.
  • The result is (address_of_ptr1 - address_of_ptr2) / sizeof(data_type).
  • This operation is only meaningful when both pointers point to elements within the same array or block of memory.

30 of 56

Dynamic memory allocation

  • Dynamic memory allocation in C refers to the process of allocating memory during program execution (runtime) rather than at compile time. 
  • This allows for more flexible memory management, especially when the required memory size is unknown until the program runs. 
  • Memory allocated dynamically resides in the heap section of memory, unlike local variables which reside on the stack.

31 of 56

Dynamic memory allocation

  • Dynamic memory allocation techniques give programmer control of memory when to allocate, how much to allocate and when to de-allocate.

32 of 56

C Dynamic Memory Allocation�

  •  an array is a collection of a fixed number of values. Once the size of an array is declared, we cannot change it.
  • Sometimes the size of the array we declared may be insufficient. To solve this issue, we can allocate memory manually during run-time. This is known as dynamic memory allocation in C programming

33 of 56

To allocate memory dynamically, library functions are 

  • malloc()
  • calloc()
  •  realloc() 
  •  free() 
  • are used.
  • These functions are defined in the <stdlib.h> header file.

34 of 56

malloc() (Memory Allocation):

  • Allocates a block of memory of a specified size (in bytes).
  • Returns a void* pointer to the beginning of the allocated block, or NULL if allocation fails.
  • The allocated memory is uninitialized and contains garbage values.

35 of 56

Malloc syntax

  • Syntax:
  • void* malloc(size_t size);
  • ptr = (int*) malloc(n * sizeof(int));

36 of 56

Malloc syntax

  • Example:
  • ptr = (int*) malloc(5 * sizeof(int));

Ptr=

GAR.VALUE

GV

GV

GV

GV

----10 bytes of memory allocated------

37 of 56

calloc() (Contiguous Allocation):

  • Allocates memory for an array of a specified number of elements, each of a specified size.
  • Initializes all allocated memory to zero.
  • Returns a void* pointer to the beginning of the allocated block, or NULL if allocation fails.

38 of 56

Calloc Syntax: �

  • Syntax:

  • void* calloc(size_t num, size_t size);

39 of 56

Calloc Syntax: �

Syntax:

  • void* calloc(size_t num, size_t size);
  • Example:
  • Ptr=(int*)calloc(5,sizeof(int));

0

1

2

3

4

Ptr=

0

0

0

0

0

------------------10 byte of memory allocated--------------

40 of 56

realloc() (Reallocation):

  • Changes the size of a previously allocated memory block.
  • It can either increase or decrease the size. If increasing, the new memory might be uninitialized.
  • Returns a void* pointer to the reallocated block, which might be at a new memory location, or NULL if reallocation fails.
  • Syntax: void* realloc(void* ptr, size_t new_size);

  • realloc() (Reallocation):
    • Changes the size of a previously allocated memory block.
    • It can either increase or decrease the size. If increasing, the new memory might be uninitialized.
    • Returns a void* pointer to the reallocated block, which might be at a new memory location, or NULL if reallocation fails.
    • Syntax: void* realloc(void* ptr, size_t new_size);

41 of 56

Realloc Syntax: �

  • Syntax:

  • void* realloc(void* ptr, size_t new_size);

42 of 56

Realloc Syntax: �

  • Syntax:
  • void* realloc(void* ptr, size_t new_size);\
  • ptr = (int *)realloc(ptr, 10 * sizeof(int));

0

1

2

3

4

5

6

7

8

9

Ptr=

0

0

0

0

0

0

0

0

0

0

----------------20 bytes memory allocated -----------------

43 of 56

free():

  • Deallocates the memory block pointed to by the given pointer, returning it to the system for reuse.

  • It is crucial to free dynamically allocated memory to prevent memory leaks.

  • realloc() (Reallocation):
    • Changes the size of a previously allocated memory block.
    • It can either increase or decrease the size. If increasing, the new memory might be uninitialized.
    • Returns a void* pointer to the reallocated block, which might be at a new memory location, or NULL if reallocation fails.
    • Syntax: void* realloc(void* ptr, size_t new_size);

44 of 56

free()Syntax:

  • Syntax:
  • void free(void* ptr);
  • free(ptr);

45 of 56

Structures

  • Structures in C programming are user-defined data types that allow you to group together variables of different data types under a single name. This enables the creation of complex data types that represent real-world entities with multiple attributes. 

46 of 56

Key characteristics of structures:

  • User-defined data type: Structures are not built-in types like int or float; you define them according to our specific needs.
  • Grouping heterogeneous data: Unlike arrays, which store elements of the same data type, structures can combine variables of different types (e.g., int, char, float, arrays) within a single unit.
  • struct keyword: The struct keyword is used to define a structure.
  • .

47 of 56

Key characteristics of structures:

  • Members: The variables declared inside a structure are called its members or fields.
  • Creating variables: Once a structure is defined, we can declare variables of that structure type, similar to declaring variables of built-in types
  • Accessing members: Members of a structure variable are accessed using the dot (.) operator. If you are working with a pointer to a structure, the arrow (->) operator is used.
  • Memory allocation: Memory for a structure is allocated when a structure variable is declared, and the size depends on the combined sizes of its members.

48 of 56

How to Create Structures in C?�

  • A structure is created outside the main function, preferably before the main function.
  • In order to access the data members of the structure, variable(s) are created either inside or outside the main function depending upon the choice of the programmer.

49 of 56

The basic syntax for a C structure is:�

  • struct struct_name {� member1_Datatype member1_name;� member2_Datatype member2_name;� ...� memberN_Datatype memberN_name;�};

50 of 56

The basic syntax for a C structure is:�

  • struct is the keyword used to define a structure.
  • struct_name is the name given to the structure.
  • member1_Datatype and member1_name are the data type and name of the first member (variable) of the structure.
  • member2_Datatype and member2_name are the data type and name of the second member (variable) of the structure.
  • ... represents additional members (variables) of the structure.
  • memberN_Datatype and memberN_name are the data type and name of the Nth member (variable) of the structure.

51 of 56

Structure Example

52 of 56

How to Create Structure Variables?�

  • As stated above, we can’t directly access the data members of a structure.
  • We need to create at least one variable to access the structure.
  • The variable is responsible for reserving a particular block of memory for the structure according to its size.

53 of 56

There are 2 ways to create a variable to access data members in a structure�

  • 1. Inside the main function:
  • struct book{char book_name[30];char author[30];int book_id;float price;};int main(){struct book b; // Here b is a variable of structure book}

54 of 56

2. Outside the main function:

  • struct book{char book_name[30];char author[30];int book_id;float price;} b; // Here b is a variable of structure book.

55 of 56

How to access Data Members in Structures in C�

  • There are basically two ways in order to access the data members in a structure through variable(s):
  • With the help of the member operator/ dot symbol (.)
  • With the help of structure pointer operator (->)

56 of 56

How to access Data Members in Structures in C�

  • Suppose we want to access the data member book_id from the structure book,
  • b.book_id;