0% found this document useful (0 votes)
2 views8 pages

Unit - 1 Array - Structure

The document explains arrays in C programming, detailing one-dimensional and two-dimensional arrays, including their declaration and initialization. It also covers structures, highlighting their ability to hold different data types and how to declare and access structure variables. Additionally, it provides examples of declaring and searching within a two-dimensional character array and discusses the differences between arrays and structures.

Uploaded by

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

Unit - 1 Array - Structure

The document explains arrays in C programming, detailing one-dimensional and two-dimensional arrays, including their declaration and initialization. It also covers structures, highlighting their ability to hold different data types and how to declare and access structure variables. Additionally, it provides examples of declaring and searching within a two-dimensional character array and discusses the differences between arrays and structures.

Uploaded by

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

1. What is Array?

In C programming, an array is a collection of elements of the same data


type that are stored in contiguous memory locations. These elements are
accessed by their index or position within the array. Arrays in C have a
fixed size, which means you need to specify the number of elements it
can hold when you declare it. The arrays are of 2 types:

a.) One-Dimensional Array


b.) Two-Dimensional Array

2. What is Two-Dimensional Array?


A two-dimensional array in C is an array that stores elements in a tabular
format, representing rows and columns. Unlike a one-dimensional array
that stores elements in a linear sequence, a 2D array is arranged in rows
and columns, forming a grid-like structure.

In C, a 2D array is declared by specifying both the number of rows and


the number of columns. The syntax for declaring a 2D array is:

dataTypearrayName[rowSize][columnSize];

Here's an example of a 2D array declaration:

int matrix[3][4]; // Declares a 3x4 matrix (3 rows and 4 columns)

You can initialize a 2D array during declaration by providing the initial


values:

int matrix[2][3] = {

{1, 2, 3},

{4, 5, 6}

};

3. Declaring& Initializing Two-Dimensional character array?


2D character arrays are very similar to 2D integer arrays. We store the
elements and perform other operations in a similar manner. A 2D
character array is more like a String array. It allows us to store multiple
strings under the same name.

A 2D character array is declared in the following manner:

char name[5][10];
The order of the subscripts is important during declaration. The first
subscript [5] represents the number of Strings that we want our array to
contain and the second subscript [10] represents the length of each
String. This is static memory allocation. We are giving 5*10=50 memory
locations for the array elements to be stored in the array.

Initialization of the character array occurs in this manner:

char name[5][10]={
"tree",
"bowl",
"hat",
"mice",
"toon"
};
Let’s see the diagram below to understand how the elements are stored
in the memory location:

The areas marked in green show the memory locations that are reserved
for the array but are not used by the string. Each character occupies 1
byte of storage from the memory.
Program to search for a string in the string array

Let’s implement a program to search for a string(a char array) entered by


the user in a 2D char array or a string array(also entered by the user):

#include <stdio.h>
#include <string.h>
intmain(){
char name[5][10],
item[10]; // declaring the string array and the character
// array that will contain the string to be matched
inti, x, f = 0;
/*taking 5 string inputs*/
printf("Enter 5 strings:\n");
for(i = 0; i< 5; i++)
scanf("%s", &name[i][0]);
/*entering the item to be found in the string array*/
printf("Enter the string to be searched:\n");
scanf("%s", &item);
/*process for finding the item in the string array*/
for(i = 0; i< 5; i++){
x = strcmp(&name[i][0],
item); // compares the string in the array with the item and if
// match is found returns 0 and stores it in variable x
if(x == 0)
f = i;
}
/*if match is not found*/
if(f == 0)
printf("the item does not match any name in the list");
/*If the match is found*/
else
printf("Item Found. Item in the array exists at index - %d", f);
return 0;
}

Output:-
Enter 5 strings:
tree
bowl
hat
mice
toon
Enter the string to be searched:
mice
Item Found. Item in the array exists at index - 3

4. What do you mean by structure?


We have seen that arrays can be used to represent a group of data items
that belong to the same type, such as int or float. However, we cannot
use an array if we want to represent a collection of data items of
different types using a single name. Fortunately, C supports a
constructed data type known as structures. A mechanism for packing
data of different types. A structure is a convenient tool for handling a
group of logically related data items. For example, it can be used to
represent a set of attributes, such as student_name, roll_number and
marks.

-- Defining a structure:

Unlike arrays, structures must be defined first for their format that may
be used later to declare structure variables. Consider the process of
structure definition and the creation of structure variable. Consider a
book database consisting of book_name, author, number of pages and
price. We can define a structure to hold this information as follows:

struct book_bank

{
char title[20];

char author[15];

int pages;

float price;

};

The keyword struct declares a structure to hold the details of four data
fields, namely title, author, pages and price. These fields are structure
elements or members. Each member may belong to a different type of
data. book_bank is the name of the structure and is called the structure
tag. The tag name may be used subsequently to declare variables that
have the tag’s structure.

-- Arrays vs Structures:

Both the arrays and structures are classified as structured data types as
they provide a mechanism that enable us to access and manipulate data
in a relatively easy manner. But they differ in a number of ways.

1. An array is a collection of related data elements of same type.


Structure can have elements of different types.
2. An array is derived data type whereas a structure is a programming
defined one.
3. Any array behaves like a built-in data type. All we have to do is to
declare an array variable and use it. But in the case of a structure, first
we have to design and declare a data structure before the variables of
that are declared and used.

-- Declaring Structure Variables:

After defining a structure format we can declare variables of that type.


A structure variable declaration is similar to the declaration of
variables of any other data types. It includes the following statements:
1. The keyword struct
2. The structure tag name.
3. List of variable names separated by commas.
4. A terminating semicolon.

E.g.:
struct book_bank
{
char title[20];
char author[15];
int pages;
float price;
};
struct book_bank book1, book2, book3;

Here, on above statement the book1, book2 and book3 are called
objects of the book_bank structure. So the all the structure tags i.e.
structure variables are call by using these object names by using
member operator .(dot).

-- Accessing structure members:

We can process and assign values to the members of a structure in a


member of ways. AS mentioned earlier the members themselves are
not variables. They should be linked to the structure variables in order
to make them meaningful members. The link between a member and
a variable is established using the member operator ‘.’ This is also
known as ‘dot operator’ or ‘period operator’.
E.g.:
book1.price;
is the variable representing the price of book1 and can be treated like
any other ordinary variable. Here is how we would assign values to
the members of book1:
book1.pages = 250;
book1.price = 120.50;
We can also use scanf to give the values through the keyboard:
scanf(“%s”,book1.title);
scanf(“%d”,&book1.pages); are valid input statements.

-- Array of Structures:

We use structures to describe the format of a number of related


variables. For example in analyzing the marks obtained by a class of
students, we may use a template to describe student name and marks
obtained in various subjects and then declare all the students as
structure variables. In such cases, we may declare an array of
structures, each element of the array representing a structure variable.

E.g.:

Struct student s1[100];

defines an array called student, that consists of 100 elements. Each


element is defined to be of the type struct student. Consider the
following declaration:

struct student
{
int rollno, s1,s2,s3,total;
float per;
};
void main( )
{
struct student s1[100];
}
This declares the student as an array of 100 elements s1[0], s1[1] and
so on..

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy