C语言 Void* when allocating



何时使用

void* space_to_use = malloc(size); 
void* space_to_use = malloc(size); 
// malloc always return void pointer that means it can be typecast to any type.
// before using void pointer it is necessary to typecast it into proper type.
// for example:-
// if size is 8 byte.It will allocate 8 byte of memory.
/*
void* space_to_use = malloc(size); 
char * ptr = (char*)space_to_use;
*/
// These two line can be combine in one statement.
char * ptr = (char*)malloc(size*sizeeof(char));
// NOTE:sizeof(char) is to make sure platform independent.
// Same for int if we want to store some integer.
 int * ptr = (int*)malloc(size*sizeeof(int));

相关内容

  • 没有找到相关文章

最新更新