0% found this document useful (0 votes)
24 views7 pages

Dbatu PPS UNIT 4

The document provides an overview of arrays and pointers in C programming, detailing their definitions, types, properties, advantages, and disadvantages. It explains how to declare and initialize arrays, the differences between one-dimensional and multi-dimensional arrays, and the various types of pointers, including integer, array, structure, function, double, float, and character pointers. Additionally, it discusses the uses of pointers in C, such as dynamic memory allocation and implementing data structures, while highlighting their advantages and potential pitfalls.

Uploaded by

rajshekar03103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views7 pages

Dbatu PPS UNIT 4

The document provides an overview of arrays and pointers in C programming, detailing their definitions, types, properties, advantages, and disadvantages. It explains how to declare and initialize arrays, the differences between one-dimensional and multi-dimensional arrays, and the various types of pointers, including integer, array, structure, function, double, float, and character pointers. Additionally, it discusses the uses of pointers in C, such as dynamic memory allocation and implementing data structures, while highlighting their advantages and potential pitfalls.

Uploaded by

rajshekar03103
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIT IV: - ARRAYS AND POINTERS IN C

ARRAYS
An array in C is a fixed-size collection of similar data items stored in contiguous memory locations. It
can be used to
store the collection of primitive data types such as int, char, float, etc., and also derived and user-
defined data types
such as pointers, structures, etc.

C Array Declaration
We can declare an array by specifying its name, the type of its elements, and the size of its
dimensions. When we
declare an array in C, the compiler allocates the memory block of the specified size to the array name.
Syntax of Array Declaration
data_type array_name [size];
or
data_type array_name [size1] [size2]...[sizeN];
where N is the number of dimensions.

Initializing arrays
Initialization in C is the process to assign some initial value to the variable. When the array
is declared or
allocated memory, the elements of the array contain some garbage value. So, we need to
initialize the array
to some meaningful value. There are multiple ways in which we can initialize an array in C.

1. Array Initialization with Declaration


In this method, we initialize the array along with its declaration. We use an initializer list to
initialize
multiple elements of the array. An initializer list is the list of values enclosed within
braces { } separated by
a comma.
data_type array_name [size] = {value1, value2, ... valueN};
2. Array Initialization with Declaration without Size
If we initialize an array using an initializer list, we can skip declaring the size of the array as
the compiler
can automatically deduce the size of the array in these cases. The size of the array in these
cases is equal to
the number of elements present in the initializer list as the compiler can automatically
deduce the size of
the array.
data_type array_name[] = {1,2,3,4,5};
The size of the above arrays is 5 which is automatically deduced by the compiler.

3. Array Initialization after Declaration (Using Loops)


We initialize the array after the declaration by assigning the initial value to each element
individually. We
can use for loop, while loop, or do-while loop to assign the value to each element of the
array.
for (int i = 0; i < N; i++) {
array_name[i] = valuei;
}

Types of Arrays in C
There are two types of arrays based on the number of dimensions it has. They are as follows:
1. One Dimensional Arrays (1D Array)
The One-dimensional arrays, also known as 1-D arrays in C are those arrays that have
only one dimension.
Syntax of 1D Array in C
array_name [size];
Example of 1D Array in C
Array of Characters (Strings)
In C, we store the words, i.e., a sequence of characters in the form of an array of
characters terminated by a NULL character. These are called strings in C language.

2. Multidimensional Arrays
Multi-dimensional Arrays in C are those arrays that have more than one dimension.
Some of the popular multidimensional arrays are 2D arrays and 3D arrays. We can
declare arrays with more dimensions than 3d arrays but they are avoided as they get very
complex and occupy a large amount of space.
A. Two-Dimensional Array in C
A Two-Dimensional array or 2D array in C is an array that has exactly two dimensions. They
can be
visualized in the form of rows and columns organized in a two-dimensional plane.
Syntax of 2D Array in C
array_name[size1] [size2];
Here,
 size1: Size of the first dimension.
 size2: Size of the second dimension.

B. Three-Dimensional Array in C
Another popular form of a multi-dimensional array is Three-Dimensional Array or 3D Array.
A 3D arrays
has exactly three dimensions. It can be visualized as a collection of 2D arrays stacked on top
of each other to
create the third dimension.

Properties of Arrays in C
It is very important to understand the properties of the C array so that we can avoid bugs
while using it. The
following are the main properties of an array in C:
1. Fixed Size
The array in C is a fixed-size collection of elements. The size of the array must be known at
the compile time
and it cannot be changed once it is declared.
2. Homogeneous Collection
We can only store one type of element in an array. There is no restriction on the number of
elements but the
type of all of these elements must be the same.
3. Indexing in Array
The array index always starts with 0 in C language. It means that the index of the first
element of the array
will be 0 and the last element will be N – 1.
4. Dimensions of an Array
A dimension of an array is the number of indexes required to refer to an element in the
array. It is the
number of directions in which you can grow the array size.
5. Contiguous Storage
All the elements in the array are stored continuously one after another in the memory. It is
one of the
defining properties of the array in C which is also the reason why random access is possible
in the array.
6. Random Access
The array in C provides random access to its element i.e we can get to a random element at
any index of the
array in constant time complexity just by using its index number.
7. No Index Out of Bounds Checking
There is no index out-of-bounds checking in C/C++, for example, the following program
compiles fine but
may produce unexpected output when run.

Advantages of Array in C
The following are the main advantages of an array:
1. Random and fast access of elements using the array index.
2. Use of fewer lines of code as it creates a single array of multiple elements.
3. Traversal through the array becomes easy using a single loop.
4. Sorting becomes easy as it can be accomplished by writing fewer lines of code.
Disadvantages of Array in C
1. Allows a fixed number of elements to be entered which is decided at the time of
declaration. Unlike a linked list, an array in C is not dynamic.
2. Insertion and deletion of elements can be costly since the elements are needed to be
rearranged after insertion and deletion.

Pointers
A pointer is defined as a derived data type that can store the address of other C
variables or a
memory location. We can access and manipulate the data stored in that memory
location using
pointers.
Syntax of C Pointers
The syntax of pointers is similar to the variable declaration in C, but we use the ( * )
dereferencing
operator in the pointer declaration.
datatype * ptr;
where,
 ptr is the name of the pointer.
 datatype is the type of data it is pointing to.

How to Use Pointers?


The use of pointers in C can be divided into three steps:
1. Pointer Declaration
In pointer declaration, we only declare the pointer but do not initialize it. To declare
a pointer, we use the ( * ) dereference operator before its name.
Example
int *ptr;
The pointer declared here will point to some random memory address as it is not
initialized. Such
pointers are called wild pointers.

2. Pointer Initialization
Pointer initialization is the process where we assign some initial value to the pointer
variable. We
generally use the ( &: ampersand ) addressof operator to get the memory address
of a
variable and then store it in the pointer variable.
Example
int var = 10;
int * ptr;
ptr = &var;

3. Pointer Dereferencing
Dereferencing a pointer is the process of accessing the value stored in the memory
address specified in the pointer. We use the same ( * ) dereferencing operator that
we used in the pointer declaration.

Types of Pointers in C
Pointers in C can be classified into many different types based on the parameter on
which we are defining their types. If we consider the type of variable stored in the
memory location pointed by the pointer, then the pointers can be classified into the
following types:
1. Integer Pointers
Syntax
int *ptr;
These pointers are pronounced as Pointer to Integer.

2. Array Pointer
Pointers and Array are closely related to each other. Even the array name is the
pointer to its first element. They are also known as Pointer to Arrays. We can create
a pointer to an array using the given syntax.
Syntax
char *ptr = &array_name;

3. Structure Pointer
The pointer pointing to the structure type is called Structure Pointer or Pointer to
Structure. It can be declared in the same way as we declare the other primitive data
types.
Syntax
struct struct_name *ptr;
In C, structure pointers are used in data structures such as linked lists, trees, etc.

4. Function Pointers
Function pointers point to the functions. They are different from the rest of the
pointers in the sense that instead of pointing to the data, they point to the code. Let’s
consider a function prototype – int func (int, char), the function pointer for this
function will be
Syntax
int (*ptr)(int, char);

5. Double Pointers
In C language, we can define a pointer that stores the memory address of another
pointer. Such pointers are called double-pointers or pointers-to-pointer. Instead of
pointing to a data value, they point to another pointer.
Syntax
datatype ** pointer_name;

6. Float Pointer
Syntax
float *ptr;
These pointers are pronounced as Pointer to Float.

7. Character Pointer
Syntax
char *ptr;
These pointers are pronounced as Pointer to character.

Uses of Pointers in C
The C pointer is a very powerful tool that is widely used in C programming to
perform various useful operations. It is used to achieve the following functionalities
in C:
1. Pass Arguments by Reference
2. Accessing Array Elements
3. Return Multiple Values from Function
4. Dynamic Memory Allocation
5. Implementing Data Structures
6. In System-Level Programming where memory addresses are useful.
7. In locating the exact value at some memory location.
8. To avoid compiler confusion for the same variable name.
9. To use in Control Tables.
Advantages of Pointers
Following are the major advantages of pointers in C:
 Pointers are used for dynamic memory allocation and deallocation.
 An Array or a structure can be accessed efficiently with pointers
 Pointers are useful for accessing memory locations.
 Pointers are used to form complex data structures such as linked lists, graphs,
trees, etc.
 Pointers reduce the length of the program and its execution time as well.

Disadvantages of Pointers
Pointers are vulnerable to errors and have following disadvantages:
 Memory corruption can occur if an incorrect value is provided to pointers.
 Pointers are a little bit complex to understand.
 Pointers are majorly responsible for memory leaks in C.
 Pointers are comparatively slower than variables in C.
 Uninitialized pointers might cause a segmentation fault.

You might also like