C Array, Array & Pointer, Array & Structure

Advertisements

Array in C

Array is a collection of similar data type. A single variable can hold only one value at a time, If we want a variable to store more than one value of same type we use array. Array is linear data structure. It can store fixed number of values.



Address of first element is random, address of next element depend upon the type of array. Here, the type is integer and integer takes two bytes in memory, therefore every next address will increment by two.

Index of array will always starts with zero.

Declaration of Array

Declaration of array means creating sequential bolcks of memory to hold fixed number of values.

Syntax of array declaration :


		Data-type Array-name[ size of Array ];

Example of array declaration :


		int Array [5];				//Statement 1

		float Array [10];				//Statement 2

In the above example, statement 1 will allocate memory for an integer array which will hold five values and statement 2 will allocate memory for float point array which will hold ten values.

Initialization of Array

Initialization means assigning value to declared Array.

Examples 1 :

		int Array [ ] = { 78, 45, 12, 89, 56 };

In the above example we are declaring and initializing an array at same time. When we declare and initialize an array at same time, giving the size of array is optional.

Examples 2 :


 			#include<stdio.h>

            void main()
            {
                  int array [5];
                  int i;

                  for(i=0;i<5;i++)                 //Input arrray from user.
                  {
                     printf("\nEnter any number : ");
                     scanf("%d",&array [i]);
                  }

                  for(i=0;i<5;i++)                 //Output arrray to console.
                  printf("%d, ",array [i]);

            }

            Output :

                  Enter any number : 78
                  Enter any number : 45
                  Enter any number : 12
                  Enter any number : 89
                  Enter any number : 56

                  78, 45, 12, 89, 56,

            

Pointer and Array

When we declare an array, compiler allocates continuous blocks of memoy so that all the elements of an array can be stored in that memory. The address of first allocated byte or the address of first element is assigned to an array name. Thus array name workes as pointer variable.

The address of first element is also known as base address.

Example : Displaying elements of an array using pointer.


       #include<stdio.h>

       void main()
       {
              int arr []={78,45,12,89,56,23,79,46,13,82};          // Array of 10 elements
              int *ptr;                                                                 // Pointer variable

              ptr = arr;                                                               // Assigning reference of array in
                                                                                          // pointer variable

              printf("\nValues : ");
              for(int a=1;a<=10;a++)
              {
                   printf("%d, ",*ptr);                                             // Displaying values of array
                                                                                           // using pointer
                   ptr++;                                                               // Incrementing pointer variable
               }
       }

   Output :

              Values : 78, 45, 12, 89, 56, 23, 79, 46, 13, 82,

In the above example statement 1 creates an array of 10 elements. Statement 2 creates a pointer variable ptr. As said above array name workes as pointer variable therefore statement 3 is assigning the address of array in pointer variable ptr. Now ptr have the address of first element in an array. Statement 4 will display the value at address of ptr. After display the first value, statement 5 incrrease the pointer variable ptr to point to next element in an array and statement 4 will display the next value in an array until the loop ends.

Array of Pointers

Array is a collection of values of similar type. It can also be a collection of references of similar type.

Syntax


              Data-type * array [size];
					

Example


       #include<stdio.h>

       void main()
       {
              int x=10,y=20,z=30;
              int *arr[3];                     // Declaring array of three pointer

              arr[0] = &x;                     // Assigning reference of x to array 0th position
              arr[1] = &y;                     // Assigning reference of y to array 1th position
              arr[2] = &z;                     // Assigning reference of z to array 2nd position

              printf("\nValues : ");
              for(int a=0;a<3;a++)
                   printf("%d, ",*arr[a]);

       }

   Output :

              Values : 10, 20, 30,

In the above example, we have declared three variable x, y, z and assigning the addresses of these variables into an array of pointer(*arr[]).

Array of Structure

Structure is collection of different data type. An object of structure represents a single record in memory, if we want more than one record of structure type, we have to create an array of structure or object. As we know, an array is a collection of similar type, therefore an array can be of structure type.

Syntax for declaring structure array


              struct struct-name
              {
                     datatype var1;
                     datatype var2;
                     - - - - - - - - - -
                     - - - - - - - - - -
                     datatype varN;
              };

              struct struct-name obj [ size ];

Example for declaring structure array


       #include<stdio.h>

       struct Employee
       {
              int Id;
              char Name[25];
              int Age;
              long Salary;
       };

       void main()
       {
              int i;
              struct Employee Emp[ 3 ];         //Statement   1

              for(i=0;i<3;i++)
              {

              printf("\nEnter details of %d Employee",i+1);

                    printf("\n\tEnter Employee Id : ");
                    scanf("%d",&Emp[i].Id);

                    printf("\n\tEnter Employee Name : ");
                    scanf("%s",&Emp[i].Name);

                    printf("\n\tEnter Employee Age : ");
                    scanf("%d",&Emp[i].Age);

                    printf("\n\tEnter Employee Salary : ");
                    scanf("%ld",&Emp[i].Salary);
              }

              printf("\nDetails of Employees");
              for(i=0;i<3;i++)
              printf("\n%d\t%s\t%d\t%ld",Emp[i].Id,Emp[i].Name,Emp[i].Age,Emp[i].Salary);

       }


   Output :

              Enter details of 1 Employee
                        Enter Employee Id : 101
                        Enter Employee Name : Suresh
                        Enter Employee Age : 29
                        Enter Employee Salary : 45000

              Enter details of 2 Employee
                        Enter Employee Id : 102
                        Enter Employee Name : Mukesh
                        Enter Employee Age : 31
                        Enter Employee Salary : 51000

              Enter details of 3 Employee
                        Enter Employee Id : 103
                        Enter Employee Name : Ramesh
                        Enter Employee Age : 28
                        Enter Employee Salary : 47000

              Details of Employees
                        101       Suresh      29      45000
                        102       Mukesh     31      51000
                        103       Ramesh    28      47000

				

In the above example, we are getting and displaying the data of 3 employee using array of object. Statement 1 is creating an array of Employee Emp to store the records of 3 employees.

Array within Structure

As we know, structure is collection of different data type. Like normal data type, It can also store an array as well.

Syntax for array within structure


              struct struct-name
              {
                     datatype var1;                    // normal variable
                     datatype array [size];          // array variable
                     - - - - - - - - - -
                     - - - - - - - - - -
                     datatype varN;
              };

              struct struct-name obj;

Example for array within structure


       struct Student
       {
              int Roll;
              char Name[25];
              int Marks[3];              //Statement 1 : array of marks
              int Total;
              float Avg;
       };

       void main()
       {
              int i;
              struct Student S;

              printf("\n\nEnter Student Roll : ");
              scanf("%d",&S.Roll);

              printf("\n\nEnter Student Name : ");
              scanf("%s",&S.Name);

              S.Total = 0;

              for(i=0;i<3;i++)
              {
                     printf("\n\nEnter Marks %d : ",i+1);
                     scanf("%d",&S.Marks[i]);

                     S.Total = S.Total + S.Marks[i];
              }

              S.Avg = S.Total / 3;

              printf("\nRoll : %d",S.Roll);
              printf("\nName : %s",S.Name);
              printf("\nTotal : %d",S.Total);
              printf("\nAverage : %f",S.Avg);
       }

   Output :

              Enter Student Roll : 10
              Enter Student Name : Kumar
              Enter Marks 1 : 78
              Enter Marks 2 : 89
              Enter Marks 3 : 56

              Roll : 10
              Name : Kumar
              Total : 223
              Average : 74.00000

				

In the above example, we have created an array Marks[ ] inside structure representing 3 marks of a single student. Marks[ ] is now a member of structure student and to access Marks[ ] we have used dot operator(.) along with object S.

Related topics

Advertisement