-->

program to understand malloc() using struct

//understanding struct pointer
#include <stdio.h>
#include <stdlib.h>
struct reco
{
    int a;
    float b;
    char c;
};
int main()
{
    struct reco *p;//pointer declaration of struct type
    p=(struct reco *) malloc (sizeof(struct reco));//memory allocation
    p->a=10;//value assignment
    p->b=3.14;//value assignment
    p->c='a';//value assignment
    printf("i=%d \nf=%f\nc= %c\n",(*p).a,(*p).b,(*p).c);//value printing
    free(p);
    return 0;
}

No comments:

Post a Comment