Home > database >  dynamic memory allocation problem with 0 as output
dynamic memory allocation problem with 0 as output

Time:12-15

I'm trying to create dynamic memory allocation using malloc but always I got 0 as output instead of 5.
My code

typedef struct{
    int nl;
    double *vect;
}vect_t;
void creerVecteur(vect_t *ptrVect){
    double *p;
    ptrVect->vect=(double *)malloc(ptrVect->nl*sizeof(double));
    ptrVect->vect[0] = 5;
    ptrVect->vect[1] = 7;
    ptrVect->vect[2] = 2;
    printf("%d\n",ptrVect->vect[0]);
}
int main(){
    vect_t v1;
    v1.nl = 3;
    creerVecteur(&v1);
}

CodePudding user response:

You're using the wrong format specifier to printf.

The %d format specifier is for printing an int in decimal format. To print a double, use %f.

  • Related