C-malloc-Rules

char *ptr without malloc

char *ptr with malloc

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

int fun_ptr_to_single_block()
{
    char a = 'A';
    char *p;

    p = &a;
    *p = 'B';

    //prints 'B'
    printf("%c\n", a);

    return 0;
}

int fun_ptr_to_single_array()
{
    char a[10] = "Bengaluru";
    char *p;

    p = a;
    p[1] = 'a';

    //prints "Bangaluru"
    printf("%s\n", a);

    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int fun_malloc_to_single_block()
{
    char *p;

    p = malloc(sizeof(char));
    *p = 'B';
    //prints 'B'
    printf("%c\n", *p);
    free(p);

    return 0;
}

int fun_malloc_to_single_array()
{
    char *p;

    p = malloc(sizeof(char)*10);
    memcpy(p, "Bengaluru", 10);
    p[1] = 'a';
    //prints "Bangaluru"
    printf("%s\n", p);
    free(p);

    return 0;
}

int *ptr without malloc

int *ptr with malloc

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

int fun_ptr_to_single_block()
{
    int a = 100;
    int *p;

    p = &a;
    *p = 200;
    //prints 200
    printf("%d\n", a); 

    return 0;
}

int fun_ptr_to_single_array()
{
    int a[5] = {10,20,30,40,50};
    int *p;

    p = a; 
    p[1] = 200;
    //prints 200
    printf("%d\n", a[1]);
    
    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int fun_malloc_to_single_block()
{
    int *p;
    
    p = malloc(sizeof(int));
    *p = 100;
    //prints 100
    printf("%d\n", *p);
    free(p);

    return 0;
}

int fun_malloc_to_single_array()
{
    int *p;

    p = malloc(sizeof(int)*5);
    p[0] = p[1] = p[2] = 100;
    p[3] = p[4] = 100;
    //prints 100
    printf("%d\n", p[1]); 
    free(p);

    return 0;
}

float *ptr without malloc

float *ptr with malloc

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

int fun_ptr_to_single_block()
{
    float a = 100;
    float *p;

    p = &a;
    *p = 200;
    //prints 200
    printf("%f\n", a);

    return 0;
}

int fun_ptr_to_single_array()
{
    float a[5] = {10,20,30,40,50};
    float *p;

    p = a; 
    p[1] = 200;
    //prints 200
    printf("%f\n", a[1]);
    
    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int fun_malloc_to_single_block()
{
    float *p;
    
    p = malloc(sizeof(float));
    *p = 100;
    //prints 100
    printf("%f\n", *p);
    free(p);

    return 0;
}

int fun_malloc_to_single_array()
{
    float *p;

    p = malloc(sizeof(float)*5);
    p[0] = p[1] = p[2] = 100;
    p[3] = p[4] = 100;
    //prints 100
    printf("%f\n", p[1]);
    free(p);

    return 0;
}

double *ptr without malloc

double *ptr with malloc

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

int fun_ptr_to_single_block()
{
    double a = 100;
    double *p;

    p = &a;
    *p = 200;
    //prints 200
    printf("%lf\n", a);

    return 0;
}

int fun_ptr_to_single_array()
{
    double a[5] = {10,20,30,40,50};
    double *p;

    p = a; 
    p[1] = 200;
    //prints 200
    printf("%lf\n", a[1]);
    
    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int fun_malloc_to_single_double()
{
    double *p;
    
    p = malloc(sizeof(double));
    *p = 100;
    //prints 100
    printf("%lf\n", *p);
    free(p);

    return 0;
}

int fun_malloc_to_single_array()
{
    double *p;

    p = malloc(sizeof(double)*5);
    p[0] = p[1] = p[2] = 100;
    p[3] = p[4] = 100;
    //prints 100
    printf("%lf\n", p[1]);
    free(p);

    return 0;
}

struct ABC *ptr without malloc

struct ABC *ptr with malloc

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

struct ABC
{
    int x;
    int y;
};

int fun_ptr_to_single_block()
{
    struct ABC a = {.x = 100, .y = 200};
    struct ABC *p;

    p = &a;
    p->x = 10;
    p->y = 20;
    //prints 10 20
    printf("%d %d\n", a.x, a.y);

    return 0;
}

int fun_ptr_to_single_array()
{
    struct ABC a[3]= {
        {.x = 100, .y = 200},
        {.x = 10, .y = 20},
        {.x = 1, .y = 2},
    };

    struct ABC *p;

    p = a;

    p[0].x = 400; p[0].y = 500;
    p[1].x = 40; p[1].y = 50;
    p[2].x = 4; p[2].y = 5;
    //prints 40 50
    printf("%d %d\n", a[1].x, a[1].y);

    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct ABC
{
    int x;
    int y;
};

int fun_malloc_to_single_block()
{
    struct ABC *p;

    p = malloc(sizeof(struct ABC));
    p->x = 10;
    p->y = 20;
    //prints 10 20
    printf("%d %d\n", p->x, p->y);
    free(p);

    return 0;
}

int fun_malloc_to_single_array()
{
    struct ABC *p;

    p = malloc(sizeof(struct ABC)*3);
    p[0].x = 400; p[0].y = 500;
    p[1].x = 40; p[1].y = 50;
    p[2].x = 4; p[2].y = 5;
    //prints 40 50
    printf("%d %d\n", p[1].x, p[1].y);
    free(p);

    return 0;
}


union ABC *ptr without malloc

union ABC *ptr with malloc

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

union ABC
{
    int x;
    int y;
};

int fun_ptr_to_single_block()
{
    union ABC a = {.x = 100, .y = 200};
    union ABC *p;

    p = &a;
    p->x = 10;
    p->y = 20;
    //prints 20 20
    printf("%d %d\n", a.x, a.y);
    
    return 0;
}

int fun_ptr_to_single_array()
{
    union ABC a[3]= { 
        {.x = 100, .y = 200},
        {.x = 10, .y = 20},
        {.x = 1, .y = 2},
    };

    union ABC *p;

    p = a;
    
    p[0].x = 400; p[0].y = 500;
    p[1].x = 40; p[1].y = 50;
    p[2].x = 4; p[2].y = 5;
    //prints 50 50
    printf("%d %d\n", a[1].x, a[1].y);
    
    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

union ABC
{
    int x;
    int y;
};

int fun_malloc_to_single_block()
{
    union ABC *p;

    p = malloc(sizeof(union ABC));
    p->x = 10;
    p->y = 20;
    //prints 20 20
    printf("%d %d\n", p->x, p->y);
    free(p);

    return 0;
}

int fun_malloc_to_single_array()
{
    union ABC *p;

    p = malloc(sizeof(union ABC)*3);
    p[0].x = 400; p[0].y = 500;
    p[1].x = 40; p[1].y = 50;
    p[2].x = 4; p[2].y = 5;
    //prints 50 50
    printf("%d %d\n", p[1].x, p[1].y);
    free(p);

    return 0;
}

char **ptr without malloc

char **ptr with malloc

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

int fun_ptr_to_single_block()
{
    char a = 'A';
    char *p;
    char **q;

    p = &a;
    *p = 'B';

    q = &p;
    **q = 'C';

    //prints 'C'
    printf("%c\n", a);

    return 0;
}

int fun_ptr_to_arrays()
{
    char a[3][10] = {
                {"Bengaluru"},
                {"c-program"},
                {"mming-lan"},
        };

    char *p[3];
    char **q;

    p[0] = a[0];
    p[1] = a[1];
    p[2] = a[2];

    q = p; // equals q = &p[0];

    printf("%s\n", q[0]);
    printf("%s\n", q[1]);
    printf("%s\n", q[2]);

    return 0;
}
#include <stdio.h>
#include <string.h>
#include<stdlib.h>

int fun_malloc_to_single_block()
{
    char **q;

    q = malloc(sizeof(q));
    *q = malloc(sizeof(*q));

    **q = 'C';
    //prints 'C'
    printf("%c\n", **q);
    free(*q);
    free(q);

    return 0;
}

int fun_malloc_to_array()
{
    char **q;

    q = malloc(sizeof(char *) * 3);

    q[0] = malloc(sizeof(char) * 10);
    q[1] = malloc(sizeof(char) * 10);
    q[2] = malloc(sizeof(char) * 10);

    memcpy(q[0], "Bengaluru", 10);
    memcpy(q[1], "c-program", 10);
    memcpy(q[2], "mming-lan", 10);

    printf("%s\n", q[0]);
    printf("%s\n", q[1]);
    printf("%s\n", q[2]);

    free(q[0]);
    free(q[1]);
    free(q[2]);
    free(q);

    return 0;
}

int **ptr without malloc

int **ptr with malloc

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

int fun_ptr_to_single_block()
{
    int a = 100;
    int *p;
    int **q;

    p = &a;
    *p = 200;

    q = &p;
    **q = 300;

    //prints 300
    printf("%d\n", a);

    return 0;
}

int fun_ptr_to_arrays()
{
    int a[3][4] = {
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12},
        };

    int *p[3];
    int **q;

    p[0] = a[0];
    p[1] = a[1];
    p[2] = a[2];

    q = p; // equals q = &p[0];

    // prints 1
    printf("%d\n", q[0][0]);
    // prints 5
    printf("%d\n", q[1][0]);
    // prints 9
    printf("%d\n", q[2][0]);

    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int fun_malloc_to_single_block()
{
    int **q;

    q = malloc(sizeof(q));
    *q = malloc(sizeof(*q));

    **q = 300;
    //prints 300
    printf("%d\n", **q);
    free(*q);
    free(q);

    return 0;
}

int fun_malloc_to_array()
{
    int **q;

    q = malloc(sizeof(int *) * 3);

    q[0] = malloc(sizeof(int) * 4);
    q[1] = malloc(sizeof(int) * 4);
    q[2] = malloc(sizeof(int) * 4);

    q[0][0] = 1;
    q[1][0] = 5;
    q[2][0] = 9;

    // prints 1
    printf("%d\n", q[0][0]);
    // prints 5
    printf("%d\n", q[1][0]);
    // prints 9
    printf("%d\n", q[2][0]);

    free(q[0]);
    free(q[1]);
    free(q[2]);
    free(q);

    return 0;
}

float **ptr without malloc

float **ptr with malloc

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

int fun_ptr_to_single_block()
{
    float a = 100;
    float *p;
    float **q;

    p = &a;
    *p = 200;

    q = &p;
    **q = 300;

    //prints 300
    printf("%f\n", a);

    return 0;
}

int fun_ptr_to_arrays()
{
    float a[3][4] = {
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12},
        };

    float *p[3];
    float **q;

    p[0] = a[0];
    p[1] = a[1];
    p[2] = a[2];

    q = p; // equals q = &p[0];

    // prints 1
    printf("%f\n", q[0][0]);
    // prints 5
    printf("%f\n", q[1][0]);
    // prints 9
    printf("%f\n", q[2][0]);

    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int fun_malloc_to_single_block()
{
    float **q;

    q = malloc(sizeof(q));
    *q = malloc(sizeof(*q));

    **q = 300;
    //prints 300
    printf("%f\n", **q);
    free(*q);
    free(q);

    return 0;
}

int fun_malloc_to_array()
{
    float **q;

    q = malloc(sizeof(float *) * 3);

    q[0] = malloc(sizeof(float) * 4);
    q[1] = malloc(sizeof(float) * 4);
    q[2] = malloc(sizeof(float) * 4);

    q[0][0] = 1;
    q[1][0] = 5;
    q[2][0] = 9;

    // prints 1
    printf("%f\n", q[0][0]);
    // prints 5
    printf("%f\n", q[1][0]);
    // prints 9
    printf("%f\n", q[2][0]);

    free(q[0]);
    free(q[1]);
    free(q[2]);
    free(q);

    return 0;
}

double **ptr without malloc

double **ptr with malloc

#include <stdio.h>
#include <string.h>
#include<stdlib.h>

int fun_ptr_to_single_block()
{
    double a = 100;
    double *p;
    double **q;

    p = &a;
    *p = 200;

    q = &p;
    **q = 300;

    //prints 300
    printf("%lf\n", a);

    return 0;
}

int fun_ptr_to_arrays()
{
    double a[3][4] = {
                {1,2,3,4},
                {5,6,7,8},
                {9,10,11,12},
        };

    double *p[3];
    double **q;

    p[0] = a[0];
    p[1] = a[1];
    p[2] = a[2];

    q = p; // equals q = &p[0];

    // prints 1
    printf("%lf\n", q[0][0]);
    // prints 5
    printf("%lf\n", q[1][0]);
    // prints 9
    printf("%lf\n", q[2][0]);

    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int fun_malloc_to_single_block()
{
    double **q;

    q = malloc(sizeof(q));
    *q = malloc(sizeof(*q));

    **q = 300;
    //prints 300
    printf("%lf\n", **q);
    free(*q);
    free(q);

    return 0;
}

int fun_malloc_to_array()
{
    double **q;

    q = malloc(sizeof(double *) * 3);

    q[0] = malloc(sizeof(double) * 4);
    q[1] = malloc(sizeof(double) * 4);
    q[2] = malloc(sizeof(double) * 4);

    q[0][0] = 1;
    q[1][0] = 5;
    q[2][0] = 9;

    // prints 1
    printf("%lf\n", q[0][0]);
    // prints 5
    printf("%lf\n", q[1][0]);
    // prints 9
    printf("%lf\n", q[2][0]);

    free(q[0]);
    free(q[1]);
    free(q[2]);
    free(q);

    return 0;
}

struct ABC **ptr without malloc

struct ABC **ptr with malloc

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct ABC
{
    int x;
    int y;
};

int fun_ptr_to_single_block()
{
    struct ABC a = {.x = 100, .y = 200};
    struct ABC *p;
    struct ABC **q;

    p = &a;
    q = &p;

    p->x = 10;
    p->y = 20;
    //prints 10 20
    printf("%d %d\n", a.x, a.y);

    (**q).x = 1;
    (**q).y = 2;

    //prints 1 2
    printf("%d %d\n", a.x, a.y);

    return 0;
}

int fun_ptr_to_arrays()
{
    struct ABC a[2][3] =
    {
        {//start of a[0]
            {.x = 100, .y = 200},
            {.x = 10, .y = 20},
            {.x = 1, .y = 2},
        },
        {//start of a[1]
            {.x = 400, .y = 500},
            {.x = 40, .y = 50},
            {.x = 4, .y = 5},
        },
    };

    struct ABC *p[3];
    struct ABC **q;

    p[0] = a[0];
    p[1] = a[1];

    q = p;

    p[0][0].x = 400; p[0][0].y = 500;
    p[1][0].x = 40; p[1][0].y = 50;
    //prints 40 50
    printf("%d %d\n", a[1][0].x, a[1][0].y);

    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

struct ABC
{
    int x;
    int y;
};

int fun_malloc_to_single_block()
{
    struct ABC **q;

    q = malloc(sizeof(struct ABC *));
    *q = malloc(sizeof(struct ABC));

    (**q).x = 10;
    (**q).y = 20;
    //prints 10 20
    printf("%d %d\n", (**q).x, (**q).y);
    free(*q);
    free(q);

    return 0;
}

int fun_malloc_to_array()
{
    struct ABC **q;

    q = malloc(sizeof(q) * 2);
    q[0] = malloc(sizeof(struct ABC) * 3);
    q[1] = malloc(sizeof(struct ABC) * 3);

    q[0][0].x = 400; q[0][0].y = 500;
    q[1][0].x = 40; q[1][0].y = 50;
    //prints 40 50
    printf("%d %d\n", q[1][0].x, q[1][0].y);

    free(q);
    free(q[0]);
    free(q[1]);
    return 0;
}

union ABC **ptr without malloc

union ABC **ptr with malloc

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

union ABC
{
    int x;
    int y;
};

int fun_ptr_to_single_block()
{
    union ABC a = {.x = 100, .y = 200};
    union ABC *p;
    union ABC **q;

    p = &a;
    q = &p;

    p->x = 10;
    p->y = 20;
    //prints 20 20
    printf("%d %d\n", a.x, a.y);

    (**q).x = 1;
    (**q).y = 2;

    //prints 2 2
    printf("%d %d\n", a.x, a.y);

    return 0;
}

int fun_ptr_to_arrays()
{
    union ABC a[2][3] =
    {
        {//start of a[0]
            {.x = 100, .y = 200},
            {.x = 10, .y = 20},
            {.x = 1, .y = 2},
        },
        {//start of a[1]
            {.x = 400, .y = 500},
            {.x = 40, .y = 50},
            {.x = 4, .y = 5},
        },
    };

    union ABC *p[3];
    union ABC **q;

    p[0] = a[0];
    p[1] = a[1];

    q = p;

    p[0][0].x = 400; p[0][0].y = 500;
    p[1][0].x = 40; p[1][0].y = 50;
    //prints 50 50
    printf("%d %d\n", a[1][0].x, a[1][0].y);

    return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

union ABC
{
    int x;
    int y;
};

int fun_malloc_to_single_block()
{
    union ABC **q;

    q = malloc(sizeof(union ABC *));
    *q = malloc(sizeof(union ABC));

    (**q).x = 10;
    (**q).y = 20;
    //prints 20 20
    printf("%d %d\n", (**q).x, (**q).y);
    free(*q);
    free(q);

    return 0;
}

int fun_malloc_to_array()
{
    union ABC **q;

    q = malloc(sizeof(q) * 2);
    q[0] = malloc(sizeof(union ABC) * 3);
    q[1] = malloc(sizeof(union ABC) * 3);

    q[0][0].x = 400; q[0][0].y = 500;
    q[1][0].x = 40; q[1][0].y = 50;
    //prints 50 50
    printf("%d %d\n", q[1][0].x, q[1][0].y);

    free(q);
    free(q[0]);
    free(q[1]);
    return 0;
}