-->

understanding malloc()

//understanding malloc()
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int *x ;//pointer declaration
    x= (int*)malloc(10*sizeof(int));//allocates the memory for 10 integers(10x4=40)
                                    // to heap

    if(x==NULL)//tests whether the memory is available
    {
        printf("error in allocating");//displays error if no memory
    }
    else
    {
    printf("%d\n", sizeof(x));//prints value 4. Because the
                            //sizeof thinks what memory each value/integer will occupy
    }
    return 0;
}

No comments:

Post a Comment