• Tech Travel Hub is your one stop, ultimate tour guide to all things tech, travel, visas and digital nomads
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
Menu
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
View Blog
April 6, 2023April 6, 2023

Concept of Array in C language

Arrays in C or Concept of Array in C language

Array is a very important concept while Learning C Programming. An array allows you to store and work with multiple values of the same data type. Learning C Programming is not completed without the concept of Array.

Arrays Hold Multiple Values

The array can hold multiple values and can be used consecutive space in the memory. It is one of the major drawbacks of the array but it also has benefits.

Ads code goes here
Benefits of Array in C:
  • Arrays represent multiple data items of the same type using a single name.
  • In arrays, the elements can be accessed randomly by using the index number.
  • Arraysallocate memory in contiguous memory locations for all its elements.

Drawbacks of Array in C:

  • Allows a fixed number of elements to be entered which is decided at the time of declaration. Unlike a linked list, an array in C is not dynamic.
  • Insertion and deletion of elements can be costly since the elements are needed to be managed in accordance with the new memory allocation.
How to Access Array Elements in C

The elements of the array can be assessed using loops i.e mostly we use for loop for this purpose. The individual elements of an array are assigned unique subscripts. These subscripts are used to access the elements. Subscript numbering in C always starts at zero. The subscript of the last element in an array is one less than the total number of elements in the array.

An Example of an Array
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array
#include <stdio.h>
int main() {
int values[5];
printf("Enter 5 integers: ");
// taking input and storing it in an array
for(int i = 0; i < 5; ++i) {
scanf("%d", &values[i]);
}
printf("Displaying integers: ");
// printing elements of an array
for(int i = 0; i < 5; ++i) {
printf("%d\n", values[i]);
}
return 0;
}

How to pass Arrays as Function Arguments in C?

C does not allow to pass an entire array as an argument to a function. However, you can pass a pointer to an array by specifying the array’s name without an index.

If you want to pass a single-dimension array as an argument in a function, you would have to declare function formal parameters.

The basic syntax for passing the array to function
void my_Function(int param[10]) {
   .
   .
   .
}
Arrays holding multiple values
#include <stdio.h>
void display(int age1, int age2)
{
    printf("%d\n", age1);
    printf("%d\n", age2);
}
int main()
{
    int ageArray[] = {2, 8, 4, 12};
    // Passing second and third elements to display()
    display(ageArray[1], ageArray[2]); 
    return 0;
}

Output on screen:

8
4

Two-Dimensional Arrays in C

A two-dimensional array is like several identical arrays put together. It is useful for storing multiple sets of data.

The declaration of 2d array looks like this in the following table below:

two-dimensional array in C

We use nested for loop generally to initialize 2d arrays.

Example of two-dimensional array in C
#include
int main(){
   /* 2D array declaration*/
   int disp[2][3];
   /*Counter variables for the loop*/
   int i, j;
   for(i=0; i<2; i++) {
      for(j=0;j<3;j++) {
         printf("Enter value for disp[%d][%d]:", i, j);
         scanf("%d", &disp[i][j]);
      }
   }
   //Displaying array elements
   printf("Two Dimensional array elements:\n");
   for(i=0; i<2; i++) {
      for(j=0;j<3;j++) {
         printf("%d ", disp[i][j]);
         if(j==2){
            printf("\n");
         }
      }
   }
   return 0;
}

Output on Screen:

Enter value for disp[0][0]:1
Enter value for disp[0][1]:2
Enter value for disp[0][2]:3
Enter value for disp[1][0]:4
Enter value for disp[1][1]:5
Enter value for disp[1][2]:6
Two Dimensional array elements:
1 2 3
4 5 6

 

Post navigation

Previous post
Next post

Umer Umer

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Simplified Concept on What Is Interface In Java: Detailed Facts In 5 Minutes
  • What is Integration Testing?
  • What is GEB-Groover Browser Functional Testing?
  • What is GEB- Detailed Facts in Just 5 Minutes
  • What is Extreme Programming(XP) Practice?

Categories

  • Adventure
  • Agile Methodology
  • All world Tour
  • Android Development
  • Ansible
  • App
  • App Development
  • Artificial Intelligence
  • AWS
  • Bitcoin
  • BlockChain
  • Blogging
  • Box Packing
  • Business
  • business
  • Business Communication
  • buying guide
  • C
  • car
  • Care
  • Certification
  • Certification
  • Chatbot
  • cinema
  • Cloud Tech
  • Content Marketing
  • Corporate
  • covid-19
  • crucible
  • Cryptocurrency
  • Database
  • DevOps
  • DevOps
  • Digital Marketing
  • Drawing
  • Dress
  • Educational
  • Employment
  • Entertainment
  • Exceptions In Java
  • Fashion
  • Festivals
  • Finance
  • Fitness
  • Food
  • Game
  • Gift ideas
  • Groovy
  • Health
  • Health
  • home
  • home improvement
  • HR
  • IllegalThreadStateException in Java
  • India Tour
  • Instagram
  • Ios
  • ISO
  • Java
  • Javascript
  • job
  • Kids Section
  • Kitchen
  • Law
  • Life Style
  • Lifestyle
  • Lifestyle Home Improvement
  • Main Stories
  • Management
  • Mattress
  • Motorable
  • Movie
  • nursing
  • Pendrive
  • Perspective
  • Pet
  • Popular
  • Programming
  • Python
  • Relationships
  • Selenium
  • SEO
  • Skin
  • SOAPUI
  • Social Media
  • Social Media Marketing
  • Software
  • software development
  • Software Development Model
  • Software Engineering
  • Software Testing
  • Sports
  • Startup
  • T Robot
  • TDD
  • Tech
  • Tech News
  • Tech News
  • Technology
  • Tool
  • Tour Guide
  • Tour Guide
  • Travel
  • UFT
  • Uncategorized
  • USB Cable
  • VPN
  • Web Development
  • Wordpress
  • Yoga
  • Youtube

Tags

Instagram Instagram Marketing Marketing SEO Thoptv App

Categories

  • Tech
  • Travel
  • Visas
  • Digital Nomad
  • Tech
  • Travel
  • Visas
  • Digital Nomad

Publishing

If you would like to publish on our site, please email admin@techtravelhub.com

Quick Links

  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact
  • Home
  • Blog
    • Technology
      • General Tech
      • Blogging
      • Java
    • Lifestyle
      • General Lifestyle
    • Travel
    • Educational
    • Business
  • About
  • Contact

Contact

Tech Travel Hub LLC

Address:
8 The Green STE A
Dover
Delaware, 19901

Phone: +1 302 956 9948