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

1 comment:

Anonymous said...

#include
#include
#include
typedef struct {
char name[20];
int age;
} Info;
Info* GetData();

int main()
{
Info kar;
Info *kar1;
kar1=&kar;
kar1=GetData();
printf("kar1 name %s, kar1 age %d",kar1->name,kar1->age);
return 0;
}
Info* GetData() {
Info *info;

info = (Info *) malloc( sizeof(Info) );
scanf("%s", info->name);
scanf("%d", &info->age);

return info;
}