小编Mat*_*att的帖子

Malloc 不为 char* 分配内存

我使用 malloc() 和字符串得到了一个简单的、意想不到的结果。代码:

int main(void) {

char* b64str;
char* binStr = "00000101";

printf("Expected size of allocated structure (b64str): 8\n");
b64str = (char*)malloc((strlen(binStr)+1)*sizeof(char));
printf("Actual size of allocated structure (b64str): %d\n\n", strlen(b64str));
Run Code Online (Sandbox Code Playgroud)

输出:

Expected size of allocated structure (b64str): 8
Actual size of allocated structure (b64str): 0
Run Code Online (Sandbox Code Playgroud)

为什么?

c string malloc char

0
推荐指数
1
解决办法
1498
查看次数

标签 统计

c ×1

char ×1

malloc ×1

string ×1