Array tutorials in c programming language by examples


Array tutorials in c programming language by examples


An array is derived data type in c programming language which can store similar type of data in continuous memory location. Data may be primitive type (int, char, float, double…), address of union, structure, pointer, function or another array.

Example of array declaration:

int arr[5];
char arr[5];
float arr[5];
long double arr[5];
char * arr[5];
int (arr[])();
double ** arr[5];

Array is useful when:

(a) We have to store large number of data of similar type. If we have large number of similar kind of variable then it is very difficult to remember name of all variables and write the program. For example:

//PROCESS ONE
#include<stdio.h>
int main(){
    int ax=1;
    int b=2;
    int cg=5;
    int dff=7;
    int am=8;
    int raja=0;
    int rani=11;
    int xxx=5;
    int yyy=90;
    int p;
    int q;
    int r;
    int avg;
    avg=(ax+b+cg+dff+am+raja+rani+xxx+yyy+p+q+r)/12;
    printf("%d",avg);

    return 0;        
}
If we will use array then above program can be written as:

//PROCESS TWO
#include<stdio.h>
int main(){
    int arr[]={1,2,5,7,8,0,11,5,50};
    int i,avg;
    for(int i=0;i<12;i++){
         avg=avg+arr[i];
    }
    printf("%d",avg/12);
    return 0;        
}

Question: Write a C program to find out average of 200 integer number using process one and two.

(b) We want to store large number of data in continuous memory location. Array always stores data in continuous memory location.

(q) What will be output when you will execute the following program?

#include<stdio.h>
int main(){
int arr[]={0,10,20,30,40};
    char *ptr=arr;
    arr=arr+2;
    printf("%d",*arr);
    return 0;        
}

Advantage of using array:

1. An array provides singe name .So it easy to remember the name of all element of an array.

2. Array name gives base address of an array .So with the help increment operator we can visit one by one all the element of an array.

3. Array has many application data structure.

Array of pointers in c:

        Array whose content is address of another variable is known as array pointers.  For example:

#include<stdio.h>
int main(){
float a=0.0f,b=1.0f,c=2.0f;
    float * arr[]={&a,&b,&c};
    b=a+c;
    printf("%f",arr[1]);
    return 0;        
}

Complex arrays in c

1. Declaration of an array of size five which can store address such functions whose parameter is void data type and return type is also void data type:


void ( arr[5] )( );

2. Declaration of an array of size five which can store address such function which has two parameter of int data type and return type is  float data type:

float ( arr[5] )(int, int);

3. Declaration of an array of size two which can store the address of printf or sacanf function:

int ( arr[2] )( const char *, … );

Note: prototype of printf function is:  int printf( const char *, … );

Different type of array in c:

(a) Array of integer
    An array which can hold integer data type is known as array of integer.

(b) Array of character
    An array which can hold character data type is known as array of character.

(c) Array of union
    An array which can hold address of union data type is known as union of integer.

For example:

(1) What will be output when you will execute the following program?

#include<stdio.h>
union A{
char p;
float const * const q;
};
int main(){
    union A arr[10];
    printf("%d",sizeof arr);
   return 0;      
}
  
Output: 20

(2) What will be output when you will execute the following program?

#include<stdio.h>
union A{
    char character;
    int ascii;
};
int main(){
    union A arr[2]={{65},{'a'}};
    printf("%c %c",arr[0],arr[1]);
       return 0;      
}
  
Output: A a

(d) Array of structure
 An array which can hold address of structure data type is known as array of structure. For example:

(1) What will be output when you will execute the following program?

#include<stdio.h>
typedef struct stu{
    char * name;
    int roll;
}s;
int main(){
    s arr[2]={{"raja",10},{"rani",11}};
    printf("%s %d",arr[0]);
    return 0;        
}

Output: raja 10

(2) What will be output when you will execute the following program?

#include<stdio.h>
struct A{
    int p;
    float q;
    long double *r;
};
int main(){
    struct A arr[10];
    printf("%d",sizeof arr);
   
    return 0;        
}

Output: 80

(e) Array of string
    An array which can hold integer data type is known as array of integer.

(f) Array of array
    An array which can hold address of another array is known as array of array.

(g) Array of address of integer
    An array which can hold address integer data type is known as array of address of integer.

Pointer to array

A pointer which holds base address of an array or address of any element of an array is known as pointer to array. For example:

(a)

#include<stdio.h>
int main(){
    int arr[5]={100,200,300};
    int *ptr1=arr;
    char *ptr2=(char *)arr;
    printf("%d   %d",*(ptr1+2),*(ptr2+4));

       return 0;       
}
Output: 300   44

(b)

#include<stdio.h>
int main(){
    static int a=11,b=22,c=33;
    int * arr[5]={&a,&b,&c};
    int const * const *ptr=&arr[1];
    --ptr;
    printf("%d ",**ptr);
        return 0;        
}

Output: 11

C tutorial home.

31 comments:

deeps said...

i have a doubt. if array stores data variables of same type, then how can we have array of structures? since structure has members of different datatypes. so isn't that a contradiction?

karthik said...

hai deeps array of structures means having more than one object with the same name
eg:

struct emp
{
int id;
float f;
};
void main()
{

struct emp e[5];
//here e is considered as array of structrues
}

Anonymous said...

can any one tell me how to count no. of occurences of number in an array

Anonymous said...

11. Define three different functions each of which accepts an array and perform (i) Bubble sort, (ii) Insertion sort
and (iii) Selection sort respectively.


please answer this

Anonymous said...

*
**
***.
how i write coding for this.

Fatih said...

#include
#include

int main()
{
int z;
printf("bir sayi girin");
scanf("%d",&z);
int i, j;
for(i = 0; i < z; i = i + 1)
{
for(j = 0; j<=i; j = j + 1)
printf("*");
printf("\n");
}
getch();
return 0;
}
yea

Anil Kachhawaha said...

//your tutorial is very nice.
//i realize there is an error in the code-
int arr[]={0,10,20,30,40};
char *ptr=arr;
arr=arr+2;
printf("%d",*arr);

//This is wrong!!
//error : LVALUE reqired

//corrtect code is:
int arr[]={0,10,20,30,40};
char *ptr=arr;
ptr=ptr+2;
printf("%d",*ptr);

sujit kumar said...

any 1 plz tell me about goto statement wid example ......:(

jyoti said...

when we declare array of structure inside array it trite as a single datatype which is called structure datatype.

Anonymous said...

Hey Guys im new to C programming and I was given an excercise by my teacher to execute the following :

Write a program that declares a 12 x 12 array of characters.
Place X’s in every other element.
Optional: User a pointer to the array to print the values to the screen in a grid format.

Please can one of the smart people on this blog assist with this one, the part where they require X,s to be placed in every other element really confuses me.

Thanks Again

krunal patel said...

#include
#include
int main(){

static int abc[12][12];
int i,j;
printf("enter values:");
for(i=0;i<12;i++)
{
for(j=0;j<12;j=j+2)
scanf("%d",&abc[i][j]);
}
for(i=0;i<12;i++)
{
for(j=0;j<12;j++)
printf("%d \t",abc[i][j]);
printf("\n");
}

getch();
return 0;
}

Anonymous said...

Hey can anyone tell me, is it necessary to declare the array at the starting of the program itself, i mean, what if we need to ask the user to enter the no of elements suppose n( i.e. size of array) and then declare the array of size n, why can't we do that?

Anonymous said...

#include

int main(void)
{
float array[30], avg = .0f, allon = .0f;
int i;
int days;


printf("Set How Many Days Will Be Calculated:");
scanf("%d", &days);

for(i = 0; i < days; i++){
printf("Give %d. Working Hours:", i+1);
scanf("%f", &array[i]);
allon += array[i];
avg = allon/days;

}

printf("Total Hours Of Working Time Period: %.1f\n", allon);
printf("Averange Working Time: %.1f\n", avg);

for(i = 0; i < days; i++)
printf("All Hours What You Set UP:%.2f", array[i]);


}

How do i get this code to give me All Hours What You Set UP: 2 3 4 5 6

Now it will give All Hours What You Set UP: 2 All Hours What You Set UP: 3 All Hours What You Set UP: 4 All Hours What You Set UP:5

Anonymous said...

while initializing the array of 2d it is necessary to mention the second dim whereas first dim is optional why it is optional pls tell

Karthik KUTTY said...

very nice.....

Karthik KUTTY said...

realy its help for me...friends you must use this website, when you have prepare for interview.
its is use full for all freshers and job seeker

Gerald Masaba said...

can anyone help me on how to do makefile using linux like for the following modules
sales.c
transport.c
report.c

Anonymous said...

Write a c program on structure that can input number, declare them, maximam, minimum, average, reverse display

Anonymous said...

i want to sort half of array in ascending order and half of array in descending order..
e.g
4 5 1 2 3 8 6 10 8 7
o/p={1 2 3 4 5 10 9 8 7 6}

JOB said...

Hey am new to C programing using arays Can any one Write for me A c program that finds the greatest number(element). the program should read the values entered by the user and uses an Array and should have 5 Elements.

Anonymous said...

i want this program
1)add duplicate from an array
[2,3,2,6,6,2]
ans 2+2+2+6+6

and
2)find no of repetation from given string of substring

Anonymous said...

array name is constant pointer you can modified it.

muhammad masood said...

This is wrong code not working properly

Birhanu Admass said...

ya

MSN Manohar said...

void main(){
short num[3][2]={3,6,9,12,15,18};
printf("%d %d",*(num+1)[1],**(num+2));
}
explanation for this program

Irshad sk said...

#include
int main()
{
int i,j;
for(i=1; i<=3; i++)
for(j=1; j<=i; j++)
printf("*");
printf("\n");

return 0;
}

Irshad sk said...

It can be done through linear search.Whenever you find the number in array increment the count.You will get the over all occurence of that number in an array.

Pankaj Prakash said...

Find it here Star(*) patterns program in C

Juspher Jhon Marco said...

MP 5 – Take Home!
Write a program that asks the user to type 10 integers of an array.
The program must compute and
write how many integers are greater than or equal to 10.
Sample Input/Output:
Enter number 1: 8
Enter number 2: 11
Enter number 3: 1
Enter number 4: 12
Enter number 5: 20
Enter number 6: 5
Enter number 7: 6
Enter number 8: 9
Enter number 9: 100
Enter number 10: 21
the number of integers greater or equal to 10 is 5.

Deepika Vivek said...

will u pls give me program for 3d for sales representation

Unknown said...

why this is not working??? plz help

#include

struct Bookinfo
{
char[20] bname;
int pages;
int price;
}book[3];

int main(int argc, char *argv[])
{
int i;

for(i=0;i<3;i++)
{
printf("\nEnter the Name of Book : ");
gets(book[i].bname);
printf("\nEnter the Number of Pages : ");
scanf("%d",book[i].pages);
printf("\nEnter the Price of Book : ");
scanf("%f",book[i].price);
}

printf("\n--------- Book Details ------------ ");

for(i=0;i<3;i++)
{
printf("\nName of Book : %s",book[i].bname);
printf("\nNumber of Pages : %d",book[i].pages);
printf("\nPrice of Book : %f",book[i].price);
}

return 0;
}