GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Tuesday, May 3, 2011

C - Malloc And Calloc

When we use malloc? We use it when we have a pointer, and we want to initialize it.
char name[] = "lady gaga";
char *ptr;
ptr = (char*) malloc(sizeof(name) * sizeof(char))

And then it's okey to initialize it through assignment or strcpy
ptr = name;
or 
strcpy(ptr, name);

Calloc is same as malloc:
ptr = (char*) calloc(sizeof(name), sizeof(char))

When you need copy an array of char to an array of char, the one way is use strcpy function
char name[] = "Indonesia";
char data[100];
strcpy(data, name);

Share/Bookmark

No comments:

Post a Comment