Page 2 :
Unit VI- Pointers, (14 Marks), Relevant CO: Develop C programs using, Pointer.
Page 3 :
Unit VI : Functions, 6.1 Concept of pointer: Declaring, initializing, 6.2 Pointer Arithmetic., 6.3 Handling arrays using Pointer., 6.4 Handling functions using Pointer., 6.5 Handling Structures using Pointer.
Page 4 :
6.1 Concept of pointer:, The pointer in C language is a variable which stores the, address of another variable. This variable can be of type int,, char, array, function, or any other pointer. The size of the, pointer depends on the architecture. However, in 32-bit, architecture the size of a pointer is 2 byte., Consider the following example to define a pointer which, stores the address of an integer., int n = 10;, int* p = &n; // Variable p of type pointer is pointing to the a, ddress of the variable n of type integer.
Page 5 :
Declaring a pointer, The pointer in c language can be declared using *, (asterisk symbol). It is also known as indirection pointer, used to dereference a pointer., Syntax:, datatype *pointer_name;, Example:, int *a; //pointer to int, char *c; //pointer to char
Page 6 :
Initialization of C Pointer variable:, Pointer Initialization is the process of assigning address of a, variable to a pointer variable. Pointer variable can only contain, address of a variable of the same data type. In C language 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., Example:, void main(), {, int a = 10;, int *ptr;, //pointer declaration, ptr = &a;, //pointer initialization, }
Page 7 :
6.2 Pointer Arithmetic:, We can perform arithmetic operations on the pointers, like addition, subtraction, etc., •, •, •, •, , Increment, Decrement, Addition, Subtraction
Page 8 :
Incrementing Pointer in C:, If we increment a pointer by 1, the pointer will start pointing to the, immediate next location. This is somewhat different from the general, arithmetic since the value of the pointer will get increased by the size of, the data type to which the pointer is pointing., #include<stdio.h>, int main(){, int number=50;, int *p;//pointer to int, p=&number; //stores the address of number variable, printf("Address of p variable is %u \n",p);, p=p+1; p++;, printf("After increment: Address of p variable is %u \n",p);, return 0;, }, , Output, Address of p variable is 3214864300, After increment: Address of p variable is 3214864302
Page 9 :
Traversing an array by using pointer:, #include<stdio.h>, void main (), {, int arr[5] = {1, 2, 3, 4, 5};, int *p = arr;, int i;, printf("printing array elements...\n");, for(i = 0; i< 5; i++), {, printf("%d ",*(p+i));, }, }, Output, printing array elements..., 1 2 3 4 5
Page 10 :
Decrementing Pointer in C, Like increment, we can decrement a pointer variable. If we decrement a, pointer, it will start pointing to the previous location. The formula of, decrementing the pointer is given below:, new_address= current_address - i * size_of(data type), , Example, #include <stdio.h>, void main(){, int number=50;, int *p;, p=&number;, printf("Address of p variable is %u \n",p);, p=p-1; p--;, printf("After decrement: Address of p variable is %u \n",p);, Output, , Address of p variable is 3214864300, After decrement: Address of p variable is 3214864299
Page 11 :
C Pointer Addition, We can add a value to the pointer variable. The formula of, adding value to pointer is given below:, new_address= current_address + (number * size_of(data, type)), #include<stdio.h>, int main(){, int number=50;, int *p;, p=&number;, printf("Address of p variable is %u \n",p);, p=p+5;, printf("After adding 3: Address of p variable is %u \n",p);, return 0;, }, , Output, Address of p variable is 3214864300, After adding 3: Address of p variable is 3214864310
Page 12 :
C Pointer Subtraction, Like pointer addition, we can subtract a value from the pointer variable., Subtracting any number from a pointer will give an address. The, formula of subtracting value from the pointer variable is given below:, new_address= current_address - (number * size_of(data type)), #include<stdio.h>, int main(){, int number=50;, int *p;//pointer to int, p=&number;//stores the address of number variable, printf("Address of p variable is %u \n",p);, p=p-5; //subtracting 3 from pointer variable, printf("After subtracting 3: Address of p variable is %u \n",p);, return 0;, }, Output, Address of p variable is 3214864300, After subtracting 3: Address of p variable is 3214864290
Page 13 :
6.3 Handling arrays using Pointer:, An array name is a constant pointer to the first element of the, array., Use a pointer to an array, and then use that pointer to access the, array elements., , Output:, 1 2 3
Page 14 :
6.4 Handling functions using Pointer:, Pointer as a function parameter is used to hold addresses of, arguments passed during function call. This is also known as call, by reference. When a function is called by reference any change, made to the reference variable will effect the original variable.
Page 16 :
6.5 Handling Structures using Pointer:, C structure can be accessed in 2 ways in a C program. They are,, Using normal structure variable, Using pointer variable, Dot(.) operator is used to access the data using normal structure, variable and arrow (->) is used to access the data using pointer, variable. You have learnt how to access structure data using, normal variable in C – Structure topic. So, we are showing here, how to access structure data using pointer variable in below C, program.
Page 17 :
Example program for c structure using pointer:, In this program, “record1” is, normal structure variable and, “ptr” is pointer structure, variable. As you know, Dot(.), operator is used to access the, data using normal structure, variable and arrow(->) is used, to access data using pointer, variable., Prrrrrr ptr->id