This question already has an answer here:
assign malloc() returnd value to a function parameter then access to outside of that function... what`s wrong here? MY sample code:
#include <stdio.h>
#include <stdlib.h>
struct student{
double cgpa;
int id;
char name[100];
};
void init(student *ptr)
{
ptr = (struct student*)malloc(sizeof(struct student));
ptr->id = 1;
ptr->cgpa = 4.00;
sprintf(ptr->name,"murad");
printf("In Initfunc:nname %snid %dncgpa %lfn",ptr->name,ptr->id,ptr->cgpa);
}
int main()
{
struct student *murad;
init(murad);
printf("IN Main:nname %snid %dncgpa %lfn",murad->name,murad->id,murad->cgpa);
free(murad);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire