Array in c


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.

An array in C Programing Language can be defined as number of memory locations, each of which can store the same data type and which can be references through the same variable name.

An array is a collective name given to a group of similar quantities. These similar quantities could be percentage marks of 100 students, number of chairs in home, or salaries of 300 employees or ages of 25 students. Thus an array is a collection of similar elements. These similar elements could be all integers or all floats or all characters etc. Usually, the array of characters is called a “string”, where as an array of integers or floats is called simply an array. All elements of any given array must be of the same type i.e we can’t have an array of 10 numbers, of which 5 are ints and 5 are floats.

Arrays and pointers have a special relationship as arrays use pointers to reference memory locations.

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
If we will use array then above program can be written as:
//PROCESS TWO
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?

Related posts:

About author

This article was written by admin

Admin has over twenty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles. Working presently as Development Manager in India. A firm Believer in Knowledge grows when it shared.

Comments

Comments (3)
  1. SBOBET says - Posted: October 24, 2012

    I know this if off topic but I’m looking into starting my own weblog and was wondering what all is needed to get setup? I’m assuming having a blog like yours would cost a pretty penny? I’m not very internet smart so I’m not 100% certain. Any tips or advice would be greatly appreciated. Thank you

  2. kasyno darmowe says - Posted: October 24, 2012

    In what exactly web host lies your web site? Due to the fact I really do not need extremely high rate world wide web, nonetheless it load in a few just a few seconds! Time to share learn when you can; )

  3. Young Lail says - Posted: October 24, 2012

    Simply desire to say your article is as amazing. The clarity in your post is simply nice and i could assume you’re an expert on this subject. Well with your permission let me to grab your feed to keep up to date with forthcoming post. Thanks a million and please carry on the rewarding work.

Leave your comment

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