×

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

Related Topics

Storage class in C

Storage class in C defines the scope, the visibility, and the lifetime of variables and functions. In other words, storage classes are used to describe the features of variables and...

3 minutes read.

Memory layout in C

Memory layout in C The C language is designed so that it becomes easier for a programmer to decide the amount of memory they want to use in a program. C program...

4 minutes read.

Sum of N numbers in C using For loop

Before we move on the program of sum of N numbers, first we have to know about the For Loop statement. The syntax of ‘for’ loop in C programming language is...

3 minutes read.

While Loop Syntax in C

What is Loop? The statements in the sequence are repeatedly executed via looping statements in C until the condition is met. The body of a loop and a control statement make...

4 minutes read.

Explain the two-way selection in C

In C programming, a two-way selection statement is used to choose between two different options based on a Boolean expression. The two-way selection statement in C is the if statement. The...

6 minutes read.

Pointer arithmetic in C

In the C programming language, a pointer is an address which stores a numeric value. Hence, a developer can perform several arithmetic operations on the same just as one does...

4 minutes read.

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

Ceil function in C

Introduction In the C Programming Language, the ceil function is a library function which is used to obtain the ceil value, i.e., it returns the smallest integer that is greater than...

4 minutes read.

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Derived Data Types in C

A variable in a program occupies some space in the computer's memory where some value is stored. Each variable in C has an associated data type. A value to be...

4 minutes read.

Write() function in c

As the name suggests the write () function is used to write the file descriptor. In other words it is used to write any file name without specifying file name,...

3 minutes read.

Armstrong Number in C

The Armstrong number is defined as the sum of each of its digits to the power of the number base for the each given number with any given number base....

3 minutes read.

Modulus on Negative Numbers in C

In this tutorial, we will explore some examples of modulus on negative number. What is Modulus of Negative number? By omitting the minus sign, one can determine the modulus of a negative...

3 minutes read.

Pyramid Pattern in C

Pyramid Pattern in C A pyramid is a polyhedron created by connecting a base that will be a polygon, and all the lateral faces are triangles. All the bases are connected...

5 minutes read.

Difference between while and do-while loop in C

Introduction Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes. Both loops process the program/code in the...

3 minutes read.

pow() function in C

pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power...

3 minutes read.

C program to Store Information of Students Using Structure

What is the Structure in C? User-defined data types include structures. Structures aid your ability to combine things of various categories into a single group. Like arrays, it operates similarly. A...

3 minutes read.

Difference between rand() and srand() function in C

What is the rand()? The rand() means random function. This function is used in C. It generates random numbers in the range of 0 to the RAND_MAX. Suppose we generate a...

3 minutes read.

Operators in C

An operator is a special symbol that tells the compiler to perform specific mathematical or logical operations. It is the combination of constants and variables through expressions. Example: int c = a+b*5 Where,...

5 minutes read.