malloc function in C programming

#include <stdio.h>
#include <stdlib.h>
typedef struct malloc_example
{
    char a;
    int i;
} MyStruct;

int main()
{
    MyStruct *sptr = malloc(sizeof(MyStruct));
    int *ptr = malloc(sizeof(*ptr));
    char *cptr = malloc(sizeof(char));

    if (ptr)
    {
        sptr->a = 'd';
        sptr->i = 321;
        *cptr = 'H';
        *ptr = 350;
        printf("The value is : %d\n", *ptr);
        printf("The value is : %c\n", *cptr);
        printf("The value of A is : %c\n", sptr->a);
        printf("The value of I is : %d\n", sptr->i);
    }
    free(ptr);
    ptr = NULL;
    return 0;
}

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *