Computer Programming C: UNIT III: Functions and Pointers

Pointers and Arrays

C Programming

The array elements can be accessed by two methods. • Standard array notation • Pointer arithmetic

POINTERS AND ARRAYS

 

The array elements can be accessed by two methods.

• Standard array notation

• Pointer arithmetic

Array notation is a form of relative addressing. The compiler treats the subscripts as a relative offset from the beginning of the array. When a pointer variable is set to the start of an array and is incremented to find the next element, absolute addressing is being implemented. Therefore, using pointers have the following advantages.

• Accessing arrays through pointers is marginally faster than using array notation.

• Pointers give better control over array processing.

• Pointers allow us to perform certain functions that are extremely difficult with array notation.

Array is a collection of similar data type elements stored under common name. When we declare an array the consecutive memory locations are located to the array of elements. The elements of an array can be efficiently accessed by using pointers.

• Array elements are always stored in consecutive memory locations according to the size of the array.

• The size of the data type with the pointer variables refers to, depends on the data type pointed by the pointer.

• A pointer when incremented, always points to a location after skipping the number of bytes required for the data type pointed to by it.

Example:

int a[5]={10,20,30,40,50}

Here, a[5] means the array 'a' has 5 elements and of integer data type.


The base address of the array start with 1th element of the array. The array is in integer type, the integer will have 2 bytes and hence the address of the next address element is incremented by 2.

Example:

int a[5]={10,20,30,40,50}

int *b;

b=a;

Here, 'b' is a pointer variable which holds the base address of the array 'a' ie. b=&a[0].


When pointer variable is incremented by 1 then the related base address will be incremented by address+2 because pointer is of type integer. Hence the next address element by increase by 2 till the size of an array.

Example:


Example 1: Write a program to print the value and address of the element.

/* Program to print the value and address of the element */

#include <stdio.h>

main()

{

/* Local definitions */

int a[5]={10,20,30,40,50};   /* a[5] has five elements of integer data type "/

int b;

/* Statements */

for (b=0;b<5;b++)

{

         printf ("The value of a[%d]=%d",b,a[b]);

         printf ("Address of a[%d]=%u",b,&a[b]);

} /* for */

} /* main */

OUTPUT

The value of a[0]=10 Address of a[0]=4000

The value of a[1]=20 Address of a[1]=4002

The value of a[2]=30 Address of a[2]=4004

The value of a[3]=40 Address of a[3]=4006

The value of a[4]=50 Address of a[4]=4008

EXPLANATION: In the above program the integer variable a is declared as an array. The for loop repeatedly executed the body up to four times starting from 0, each time the loop prints the value of variable a assigned is to array and prints the address of variable a as shown in output.


Example 2: Write a program to print the value and the address of the element using pointer.

* Program to print the value and the address of the element using pointer */

#include <stdio.h>

main()

{

int arr[5]={10,20,30,40,50}; /* arr[5] has five elements of integer data type */

int i,*p_arr;    /* i,*p_arr is a pointer variables */

p_arr=arr;

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

{

        printf("Address=%u", &arr[i]);

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

        printf("%d", *(arr+i));

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

        printf(“%d”, *(p_arr+i));

} /* for */

} /* main */

OUTPUT

Address 4000 Element 10 10 10 10

Address 4002 Element 20 20 20 20

Address 4004 Element 30 30 30 30

Address 4006 Element 40 40 40 40

Address 4008 Element 50 50 50 50

EXPLANATION: In the above program, the variable arr[] is declared as integer array, the elements are displayed using different syntax. (1) &arr[i], (2) *(arr+i), (3) p_ (4) *(p_arr+i).

1) &arr[i] ‒ This prints 0th address elements from the array.

2) *(arr+i) ‒ The arr+i is addition of a constant with the base address of an array. It shows the address of element. The *(arr+i) points to the elements of an array.

3) p_arr[i] ‒ Here 'p_arr' refers to the element number and i refers to the base address. By varying p_arr and i the various elements of the array are displayed.

4) *(p)arr+i) ‒ This statement is same as 2.

Example 3: Write a program to add the sum of the numbers using pointer.

/* Program to add the sum of number using pointer */

#include <stdio.h>

main()

{

 /* Local definitions */

int b, total;

int a[5]; /* a[5] is an array elements */

int *c; /**c is a pointer variable */

/* Statements */

for (b=0;b<5;b++)

{

           printf ("Enter the number %d", b+1);

          scanf ("%d", &a[b]);

} /* for */

c=a,

for (b=0;b<5;b++)

{

        printf ("%d", *c);

        total = total + *c;

        с = c + 1;

} /* for */

printf ("\n Total = %d", total);

} /* main */

OUTPUT

Enter the number 10 20 30 40 50

Total = 150

EXPLANATION: In the above program, the variable a is declared as an integer type array. c is declared as a pointer variable. The for loop reads the 5 integer elements from the keyboard. The array a is assigned to pointer variable c. The second for loop gets each element of array through pointer variable c and adds to the variable total. Finally the variable total is printed as output.

Example 5: Write a program to sort a given number using pointer.

* Program to sort a given number using pointer */

#include <stdio.h>

main()

{

int j,k;

int arr[5]; /*arr[5] is a array elements */

int *a;  /* *a is a pointer variable */

a=arr;

for(j=0;j<5;j++)

{

       printf("Enter the number %d="j+1);

       scanf("%d", &arr[j]);

} /* for */

printf("\n Before sorting");

for(j=0;j<5;j++)

         printf("%d\n", arr[j]);

a=arr;

for(j=0;j<5;j++)

for(k=0;k<5‒j−1;k++)

      If (*(arr+k) > *(arr+k+1))

swap (arr+k, arr+k+1); /* function call */

printf ("After sorting");

for (j=0; j<5; j++)

            printf ("%d\n", arr[j]);

} /* main */

swap (b,c)

int *b, *c;   /* *b, *c is a point variables */

{

        int temp;

        temp=*b;

        *b=*c;

        *c=temp;

} /* Swap */

OUTPUT

Enter the number=8

Enter the number=5

Enter the number=7

Enter the number=4

Enter the number=6

Before sorting  8 5 7 4 6

After sorting 4 5 6 7 8

EXPLANATION: The variable arr is assigned to pointer variable a. The body of for loop j and k checks the condition (*(arr+k)>*(arr+k+i). If the condition is true the program control transfer to swap() function. The swap function interchanges the value using third variable temp then control transfer to main program. Finally using for loop j the sorted output is printed.

 

Computer Programming C: UNIT III: Functions and Pointers : Tag: Computer Science : C Programming - Pointers and Arrays


Computer Programming C: UNIT III: Functions and Pointers



Under Subject


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation



Related Subjects


English Essentials I

EN25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


தமிழர் மரபு - Heritage of Tamils

UC25H01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Calculus

MA25C01 Maths 1 M1 - 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I

PH25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Applied Chemistry I

CY25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Makerspace

ME25C04 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming C

CS25C01 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Computer Programming Python

CS25C02 1st Semester | 2025 Regulation | 1st Semester 2025 Regulation


Fundamentals of Electrical and Electronics Engineering

EE25C03 1st Semester EEE Depart | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Mechanical Engineering

ME25C03 1st Semester Mechanical Dept | 2025 Regulation | 1st Semester 2025 Regulation


Introduction to Civil Engineering

CE25C01 1st Semester Civil, Agri Departments | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing

CS25C03 1st Semester - AIDS, CSE, CSE(CY), IT Department | 2025 Regulation | 1st Semester 2025 Regulation


Applied Physics I Laboratory

PH25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Applied Chemistry I Laboratory

CY25C01 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming C Laboratory

CS25C01 1st Semester EEE, ECE, CSE, CSE(CY), AIDS, IT practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Computer Programming Python Laboratory

CS25C02 1st Semester practical Laboratory Manual | 2025 Regulation | 1st Semester Laboratory 2025 Regulation


Engineering Drawing

ME25C01 EEE, Mech, Agri, EEE Depts | 2025 Regulation | 2nd Semester 2025 Regulation


Basic Electronics and Electrical Engineering

EE25C04 1st Semester ECE Dept | 2025 Regulation | 1st Semester 2025 Regulation


Essentials of Computing - Laboratory

CS25C03 1st Semester AIDS, CSE, CSE(CY), IT Depts | practical Laboratory Manual | 2025 Regulation | 1st Semester 2025 Regulation