What Is An Array? Array
What Is An Array? Array
What is an Array?
An array is a data structure that stores a collection of elements, all of the same type, in a
contiguous block of memory. It allows you to store multiple values in a single variable, with
each element being accessed by its index (position in the array).
1. Fixed Size: The size of an array is fixed at the time of its creation. This means you
cannot change the size of an array after it's initialized
2. Homogeneous: All elements in an array must be of the same data type
3. Indexed: Each element in an array is accessed using an index. In most programming
languages, array indexing starts at 0.
Example:
In this example:
Example :
In C, a 2D array is a matrix-like structure, where elements can be stored in a grid format (like
a table). It can be thought of as an array of arrays, where each element in the main array
represents a row, and each row contains a set of values (columns).
2D Array Initialization
When you know the values you want to store in the array, you can initialize a 2D array at the
time of declaration by specifying both the row and column sizes, and providing the values in
a matrix format.
Syntax:
data_type array_name[row_size][column_size] = {
{value1, value2, ..., valueN},
{value1, value2, ..., valueN},
...
};
Example:
#include <stdio.h>
int main() {
// Declare and initialize a 2D array (3 rows and 4 columns)
int arr[3][4] = {
{1, 2, 3, 4}, // First row
{5, 6, 7, 8}, // Second row
{9, 10, 11, 12} // Third row
};
return 0;
}
Output:
1234
5678
9 10 11 12
In this example:
In dynamic initialization, you can define the array with a fixed size but initialize the values
during the execution of the program (runtime). You can achieve this by assigning values to
each element in a loop.
Example:
#include <stdio.h>
int main() {
int arr[3][4]; // Declare a 2D array with 3 rows and 4 columns
Output:
1234
5678
9 10 11 12
In this example:
Characteristics of a String in C:
String Initialization in C
There are different ways to initialize a string in C. Here are the common methods:
1. Static Initialization
This is the most common way of initializing a string, using a string literal.
Syntax:
In this case:
Example:
#include <stdio.h>
int main() {
return 0;
}
Output
String: Hello, World!
In this example:
You can also initialize a string by specifying each character individually and adding the null
terminator manually.
Example:
#include <stdio.h>
int main() {
return 0;
}
Output:
String: Hello
Q.4 WAP to calculate average of 5 numbers.
#include <stdio.h>
int main()
{
average = sum / 5;
1.strlen()
Purpose: Computes the length of a string (excluding the null terminator \0).
Syntax: strlen(str);;
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
printf("Length of string: %d\n", strlen(str)); // Output: 13
return 0;
}
Explanation: The strlen() function returns the number of characters in the string before the
null terminator.
2. strcpy()
Example:
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello, World!";
char dest[50];
strcpy(dest, src); // Copy src to dest
printf("Source: %s\n", src); // Output: Hello, World!
printf("Destination: %s\n", dest); // Output: Hello, World!
return 0;
}
Explanation: The strcpy() function copies the contents of src into dest.
3. strncpy()
#include <stdio.h>
#include <string.h>
int main() {
char src[] = "Hello, World!";
char dest[50];
return 0;
}
Explanation: The strncpy() function copies up to n characters from the source string to the
destination string. In this case, it copies only the first 5 characters.
4. strcat()
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";
return 0;
}
Explanation: The strcat() function appends the src string to the dest string. Note that dest
must have enough space to accommodate the resulting concatenated string.
5. strncat()
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";
return 0;
}
Explanation: The strncat() function appends the first n characters from src to dest.
6. strcmp()
Return Value:
Example
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
if (result == 0) {
printf("Strings are equal.\n");
} else if (result > 0) {
printf("str1 is greater than str2.\n");
} else {
printf("str1 is less than str2.\n");
}
return 0;
}
Explanation: The strcmp() function compares two strings character by character and returns
a value indicating their lexicographical order.
2. Data Type:
Array: All elements in an array must be of the same type (e.g., all integers, all floats).
Structure: Members of a structure can be of different types (e.g., a structure can have
an integer, a float, and a string).
3. Memory Allocation:
Array: Memory is allocated in a contiguous block for all elements. The size of the
array is fixed (if it's statically allocated).
Structure: Memory is allocated for each member according to its data type. The
members may not necessarily be contiguous, and the size of the structure is the sum of
the sizes of its members, plus any padding needed for alignment.
4. Accessing Elements:
Array: Elements in an array are accessed using an index. For example, array[0] refers
to the first element in the array.
Structure: Members in a structure are accessed using the member name. For
example, person.name accesses the name member of a structure person.
5. Purpose:
Array: Typically used when you want to store multiple values of the same type (e.g.,
a list of numbers, a list of names).
Structure: Useful when you need to represent a complex entity that has different
properties (e.g., a person's name, age, and height).
Example in C:
Array:
Structure:
struct Person {
char name[50];
int age;
float height;
};
int main() {
char s[] = "abcde";
return 0;
}
Q.8 WAP to calculate the length of string using built in function
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "Geeks";
return 0;
}
Q.9 WAP to demonstrate strcat () function.
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[] = "world!";
strcat(str1, str2);
return 0;
}