mardi 7 juillet 2020

Why in case of structures the sizeof operator does not behave properly when we dynamically create memory for a structure array?

I am making a array of structure dynamically.And trying to get the number of elements stored in that structure array by using sizeof operator.But it is giving me the wrong answer.

struct
    {
        int *d,*e,f,count;
    }ramanujan;              defining a structure
    
    
    
    int main()
    {
    
        ramanujan u[5];
        printf("%d",sizeof(u));              Result is 120 


        ramanujan *u=(ramanujan*)malloc(sizeof(ramanujan)*5);
        printf("%d",sizeof(u));              We expect 120 but unfortunately it's 8
    }

Aucun commentaire:

Enregistrer un commentaire