×

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

Factorial Program in C using For Loop

Before move on the factorial program. We have to know about the for loop in C programming language. The syntax of the For Loop is: for (initialization statement; test expression; an increment...

3 minutes read.

Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs. Firstly, we can learn the range of int: The int is...

3 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Recursion in C

Recursion in C: In the C programming language, the concept known as recursion exists that is a technique in which a function calls itself either directly or indirectly. It allows...

4 minutes read.

Types Of Structures In C

We can normally store elements of the same datatype with the help of an array in C programming. We can store multiple numbers of elements of a character data type...

3 minutes read.

Pre-increment and Post-increment in C

The rich set of operators is supported by the c language. In c there are several operators used. The mathematical operations in the programming language are done with the operators...

4 minutes read.

C Array

An array is a collection of elements that are used to hold the fixed number of values of the same type. We cannot change the size and type of array...

2 minutes read.

FIFO Example in the C Language

FIFO is an acronym that means "First In, First Out." It is a data structure handling method in which the first element is handled first, and the last element is...

3 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

2 minutes read.

Type Casting in C

Type casting is a way to convert a variable from one data type to another data type.Syntax: (type) expression;     Example: #include <stdio.h> int main() { int a,b; float sum; printf("Example of Type Casting\n"); printf("Enter any integer nos:"); scanf("%d",&a,&b); sum=a+b; printf("Sum: %f",sum); return 0; } Output Example...

1 minute read.

fopen() function in C

fopen() function, is one of the file handling functions. It is used to open the existing file and perform operations on the file. If the file does not exist, it...

3 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes read.

C Decision control statement

Decision Making C conditional statements are used to specify one or more conditions. The statements will be executed if the condition becomes true otherwise alternative statement will be executed if the...

3 minutes read.

memcpy() in C

Introduction: Here we discuss about the memcpy() function in C. The memcpy() function is also called the Copy Memory Block function. Used to create a copy of a specific drawing...

4 minutes read.

Difference between while and for loop in C

While Loop The syntax that can be used for the ‘while’ loop in the C programming language is mentioned below: while (test expression or termination condition)  {   // the main body of the...

6 minutes read.

Do-While Loop in C

Both 'While' and 'Do-While' loops in C mostly have a similar concept, and the code of both loops runs for mainly similar purposes in the same manner. But, there is...

3 minutes read.

Bitwise XOR Operator in C

What is Bitwise? As we know, bits are the smallest unit of a number and are mostly used in a computing system. So, in bitwise, we do operations on bits instead...

3 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.

Socket Programming in C

Socket programming is a process that connects the two or more nodes on a networking communication with respective to each other. One of the sockets that determines the socket (I.e.,...

5 minutes read.