Escolar Documentos
Profissional Documentos
Cultura Documentos
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:
//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;
}
(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;
}
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.
Array
of
pointers
in
c:
Complex arrays in c
void ( arr[5] )( );
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
(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
Pointer
to
array
(a)
#include<stdio.h>
int main(){
int arr[5]={100,200,300};
int *ptr1=arr;
char *ptr2=(char *)arr;
printf("%d
return 0;
}
%d",*(ptr1+2),*(ptr2+4));
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