Function returning pointer to structure in c programming.

typedef struct film{
    int size;
    int pixel;
    float price;
}xyz,pqr;
struct film *jadu(){
    static xyz one={231,12,900.0},*p=&one;
    return p;
}
void main(){
    pqr *ptr;
    ptr=jadu();
    clrscr();
    printf("%d",ptr->pixel);
    getch();
}
Output:12

ONE MORE EXAMPLE:




typedef struct book{
char *name;
int id;
float price;
}BOOK;
BOOK show();
void main(){
BOOK list;
list=show();
clrscr();
printf("%s %d ",list.name,list.id);
getch();
}
BOOK show(){
BOOK list1={"Advance C",200,845.0f};
BOOK list2={"Basic C",210,130.0f};
if(list1.price>list2.price)
return list1;
else
return list2;
}
Output: Advance C 200
Post a Comment