Saturday 1 February 2014

Arrays questions

(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
(3) 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
#include<stdio.h>
int main(){
    int arr[3]={10,20,30};
    int x=0;
    x = ++arr[++x] + ++x + arr[--x];
    printf("%d ",x);
    return 0;
}
Output: 44
#include<stdio.h>
int main(){
    int a[]={10,20,30,40};
    int i=3,x;
    x=1*a[--i]+2*a[--i]+3*a[--i];
    printf("%d",x);
    return 0;
}
Output: 100
#include<stdio.h>
int main(){
    static int a[][2][3]={0,1,2,3,4,5,6,7,8,9,10,11,12};
    int i=-1;
    int d;
    d=a[i++][++i][++i];
    printf("%d",d);
    return 0;
}
Output: 10

#include<stdio.h>
int main(){
    static double *p,*q,*r,*s,t=5.0;
    double **arr[]={&p,&q,&r,&s};
    int i;
    *p=*q=*r=*s=t;
    for(i=0;i<4;i++)
        printf("%.0f  ",**arr[i]);
    return 0;
}
output error