Displaying Array in C
- Reference a collection of variables of similar data type in an array using a single element.
- The parts are stored next to each other.
- When declaring an array, you must specify its size.
- Each element in the array is counted from zero.
- You can access an element using its position in the array.
- Arrays have one or more dimensions.
In C or other programming languages, an array is a collection of similar data items stored in contiguous memory locations. Elements can be accessed randomly via an array index. It cannot store arbitrary collections of primitive data types such as int, float, double, and char. You can also store structures, pointers, and other derived data types in arrays.
Why do You Need an Array?
If you have a small number of objects, you can use regular variables (v1, v2, v3, ...), but if you store many instances, regular variables become unmanageable. The idea of ??arrays is to represent many instances in one variable.
Pros:
Code optimization: Data can be fetched or sorted efficiently. Direct access: You can get the data at the index position
Cons:
Size Constraints: Only fixed sizes of elements in arrays can be stored. It doesn't grow at runtime.
Array displaying by specifying size:
#include <stdio.h>
int main(void)
{
// Array declaration by specifying size
int arr1[10];
// With recent C/C++ versions, we can also
// declare an array of user specified size
int n = 10;
int arr2[n];
return 0;
}
Array declaration by initializing the elements:
// Array declaration by initializing elements
#include <stdio.h>
int main(void)
{
int arr[] = { 10, 20, 30, 40};
// Compiler creates an array of size 4.
// above is same as "int arr[4] = {10, 20, 30, 40}"
return (0);
}
Array declaration by specifying the size and initializing elements:
#include <stdio.h>
int main(void)
{
// Array declaration by specifying size and initializing
// elements
int arr[6] = { 10, 20, 30, 40 };
// Compiler creates an array of size 6, initializes first
// 4 elements as specified by user and rest two elements as
// 0. above is same as "int arr[] = {10, 20, 30, 40, 0, 0}"
return (0);
}
Advantages of using arrays in C/C++:
Elements can be accessed randomly via an array index. Fewer lines of code are required as it creates a single array with multiple elements. Easy access to all components It simplifies array iteration in a single loop. This can be achieved with fewer lines of code, making sorting easier.
Drawbacks of using arrays in C/C++:
You can enter a given number of elements in the declaration phase. Unlike linked lists, C arrays are not dynamic. Inserting and deleting items can be expensive, as the items must be managed according to the new storage allocation. Information about arrays in C/C++:
To get the array elements:
Integer indices are used to access array elements. Array indices start at 0 and range up to thesize of the array minus 1. The first element of the array is also included in the array name.
#include<stdio.h>
int main()
{
int i, arr[5];
/* Reading Array */
printf(“Enter 5 numbers:\n”);
for (i=0; i< 5; i++)
{
printf(“arr[%d] = ” , i );
scanf(“%d”, &arr[i]);
}
/* Displaying array content */
printf(“Array content is:\n”);
for (i=0; i< 5; i++)
{
printf(“arr[%d] = %d\n”, i, arr[i] );
}
return 0;
}
Output:
Enter 5 numbers:
arr[0] = 1?
arr[1] = 2?
arr[2] = 3?
arr[3] = 4?
arr[4] = 5?
Array content is:
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Define Pointer
A pointer is a variable whose value is the address of another variable or a direct memory location address. Before you can use a pointer to store the address of a variable, you must declare the pointer, just as you must declare any variable or constant the *var-name type is the most common way to declarepointer variables. Thebase typeof the pointer, in this case, is typical. The pointer variable name var-name must be a valid C data type. The same asterisk used in pointer declarations is used for multiplication.
On the other hand, an asterisk isused in this statement to indicate a variable as a pointer. The "address of a variable" is provided by the unary operator or the unary operator & The "contents of the object pointed to by the pointer" is provided by the indirection or dereferencing operator * Below is the source code for a C program that uses pointers to point to an array of addresses and compiles and runs successfullyon a Windows system to produce the desired output.
/* C Program to display array with addresses using pointers */
#include<stdio.h>
#include<stdlib.h>
#define size 10
int main() {
int a[3] = { 11, 22, 33 };
printf("\n a[0] ,value=%d : address=%u", a[0], &a[0]);
printf("\n a[1] ,value=%d : address=%u", a[1], &a[1]);
printf("\n a[2] ,value=%d : address=%u", a[2], &a[2]);
return (0); }
Output:
a[0] ,value=11 : address=6356740
a[1] ,value=22 : address=6356744
a[2] ,value=33 : address=6356748