您好,欢迎访问一九零五行业门户网

在C语言中,malloc函数是用来动态分配内存的

malloc()函数代表内存分配,动态分配一块内存。
它保留指定大小的内存空间,并返回指向内存位置的空指针。
malloc() 函数携带垃圾值。返回的指针是void类型。
malloc()函数的语法如下 -
ptr = (casttype*) malloc(size);
示例以下示例展示了 malloc() 函数的用法。
 现场演示
#include<stdio.h>#include<string.h>#include<stdlib.h>int main(){ char *memoryalloc; /* memory allocated dynamically */ memoryalloc = malloc( 15 * sizeof(char) ); if(memoryalloc== null ){ printf("couldn't able to allocate requested memory
"); }else{ strcpy( memoryalloc,"tutorialspoint"); } printf("dynamically allocated memory content : %s
", memoryalloc); free(memoryalloc);}
输出当执行上述程序时,会产生以下结果 -
dynamically allocated memory content: tutorialspoint
以上就是在c语言中,malloc函数是用来动态分配内存的的详细内容。
其它类似信息

推荐信息