Passing Structure Vable in Function Using Pointer

3
Passing Structure Variable in function using pointer #include<stdio.h> struct rec { float pi; char a; int i; }; void show(struct rec *ptr); int main() {int j; struct rec * ptr[5]; fflush(stdin); for(j=0;j<5;j++) { printf("int,float,char,"); ptr[j] =(struct rec *) malloc (sizeof(struct rec)); scanf("%d",&ptr[j]->i); scanf("%f",&ptr[j]->pi); fflush(stdin); scanf("%c",&ptr[j]->a); show(ptr[j]); }

description

thh

Transcript of Passing Structure Vable in Function Using Pointer

Page 1: Passing Structure Vable in Function Using Pointer

Passing Structure Variable in function using pointer

#include<stdio.h>

struct rec

{

float pi;

char a;

int i;

};

void show(struct rec *ptr);

int main()

{int j;

struct rec * ptr[5];

fflush(stdin);

for(j=0;j<5;j++)

{

printf("int,float,char,");

ptr[j] =(struct rec *) malloc (sizeof(struct rec));

scanf("%d",&ptr[j]->i);

scanf("%f",&ptr[j]->pi);

fflush(stdin);

scanf("%c",&ptr[j]->a);

show(ptr[j]);

}

return 0;

}

Page 2: Passing Structure Vable in Function Using Pointer

void show(struct rec *ptr)

{int j;

printf("First value: %d\n", ptr->i);

printf("Second value: %f\n", ptr->pi);

printf("Third value: %c\n", ptr->a);

}

Passing address of array of pointers(pointing to Structure Variable )in function using pointer

#include<stdio.h>

struct rec

{

float pi;

char a;

int i;

};

void show(struct rec *ptr[2]);

int main()

{int j;

struct rec * ptr[2];

fflush(stdin);

for(j=0;j<2;j++)

{

printf("int,float,char,");

ptr[j] =(struct rec *) malloc (sizeof(struct rec));

scanf("%d",&ptr[j]->i);

Page 3: Passing Structure Vable in Function Using Pointer

scanf("%f",&ptr[j]->pi);

fflush(stdin);

scanf("%c",&ptr[j]->a);}

show(ptr);

getch();

return 0;

}

void show(struct rec *ptr[2])

{int j;

for(j=0;j<2;j++)

{

printf("First value: %d\n", ptr[j]->i);

printf("Second value: %f\n", ptr[j]->pi);

printf("Third value: %c\n", ptr[j]->a);

}

}