0% found this document useful (0 votes)
152 views

2.0 - Pointers and Arrays

This document discusses pointers and arrays in C programming. It begins by introducing pointers and how arrays are stored contiguously in memory. It then discusses how to use pointers to access array elements, including declaring a pointer variable to point to an array, incrementing the pointer to traverse the array, and using array names as pointers. Examples are provided to demonstrate using pointers to display arrays in reverse order, add array elements, demonstrate pointer increment/decrement, and copy an array. The document serves as teaching material for a course on pointers and linear data structures.

Uploaded by

sangeetha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views

2.0 - Pointers and Arrays

This document discusses pointers and arrays in C programming. It begins by introducing pointers and how arrays are stored contiguously in memory. It then discusses how to use pointers to access array elements, including declaring a pointer variable to point to an array, incrementing the pointer to traverse the array, and using array names as pointers. Examples are provided to demonstrate using pointers to display arrays in reverse order, add array elements, demonstrate pointer increment/decrement, and copy an array. The document serves as teaching material for a course on pointers and linear data structures.

Uploaded by

sangeetha
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

20CST21 – Programming and Linear Data Structures

UNIT I : Pointers and Arrays, Pointers


and Strings
By
Ms.M.Sangeetha
Asst. Professor (SRG) / CSE
Kongu Engineering College
Perundurai, Erode, Tamilnadu, India

Thanks to and Resource from : Sumitabha Das, “Computer Fundamentals and C Programming”, 1st Edition, McGraw Hill, 2018.
Unit I : Contents
1. Pointers- Introduction
2. Pointers and 1D array
3. Passing an array to a function
4. Returning an array from function
5. NULL pointers
6. Array of pointers
7. Pointer-to-pointer
8. Pointers and 2D array
9. Generic pointers
10.Dangling Pointer
11.Using Pointers for string manipulation
12.Two dimensional array of strings
13.Array of pointers to strings.
04/28/2021 2.0 _ Pointers and Arrays 2
Pointers and Arrays
• We are already familiar with arrays, which store multiple values of the
same data type.
• Array elements are stored in contiguous memory locations.
• When an array is declared, compiler allocates required amount of
contiguous memory locations to hold all the elements of the array.
• Memory is allocated by the compiler during compilation time itself.
• Base address of the array is the address of the first element of the array.
• We can create a pointer which can be used to point an array.
• In an one dimensional array, we can use single subscript to access the
array elements.
• Name of the 1-D array or &array[0] points to the address of the first
element of an array.

04/28/2021 2.0 _ Pointers and Arrays 3


Pointers and 1D Arrays
• Suppose we declare an integer array “A” with 5 elements
and assuming that the base address of the array a is 1000
and each integer element requires two bytes of memory.
• The five elements will be stored as follows:

• Here array name “A” acts as a constant pointer which will


give the base address of the array and points to the first
element of the array, A[0].
– i.e., the value of A and &A[0] are same.

04/28/2021 2.0 _ Pointers and Arrays 4


Pointers and 1D Arrays

• We can declare another pointer of type int to point to the array


A.

• Now we can access every element of the array A using pointer p.


• (*p) is used to access the element A[0] which has the value 1 in
the given example.
• To access the next element in the array A,
– increment the pointer p by 1 using (p++) or (++p).
– Now we can give *p to get A[1].
– It gives the value 2.
– To get the next element in the array A, increment the pointer p by 1 and
use *p.
04/28/2021 2.0 _ Pointers and Arrays 5
Example 1- Program to demonstrates the use of
pointer to access the array elements

04/28/2021 2.0 _ Pointers and Arrays 6


Example 2- use array name as a pointer to
access the array elements

04/28/2021 2.0 _ Pointers and Arrays 7


• In the above program, A is a constant pointer which always
points to first element A[0].
• We cannot move this pointer to next element using ++
operator since base address of the array cannot be changed.
• To access the elements of the array using a constant pointer A,
we can use the following statements:

04/28/2021 2.0 _ Pointers and Arrays 8


Example 3- Program to display the elements of
an array in reverse order using pointer.

04/28/2021 2.0 _ Pointers and Arrays 9


Example 4 – Program to add the elements of an
array using pointer

04/28/2021 2.0 _ Pointers and Arrays 10


Example 5 – Program to demonstrate increment and
decrement operators with pointers and arrays.

04/28/2021 2.0 _ Pointers and Arrays 11


Example 6 - Program to copy an array to another array
using pointers

04/28/2021 2.0 _ Pointers and Arrays 12


Thank you

04/28/2021 2.0 _ Pointers and Arrays 13

You might also like