c - 为什么当我打印时,十进制的 *str 显示为 80(ASCII 字母 P)?

标签 c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
   char *str;

   /* Initial memory allocation */
   str = (char *) malloc(15);

   strcpy(str, "tutorialspoint");
   printf("String = %s,  Address = %u\n", str, str);

   /* Reallocating memory */
   str = (char *) realloc(str, 25);

   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);

   /* Deallocate allocated memory */
   free(str);

   printf("\n%d",*str);
   return(0);

}

最佳答案

您的最后一个打印表示将 str 指向的字符打印为十进制,这正是它所做的(对于您刚刚释放的内存位置,使得此未定义的行为)。

关于c - 为什么当我打印时,十进制的 *str 显示为 80(ASCII 字母 P)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59457522/

相关文章:

c - 在 Windows 上诊断 C 应用程序崩溃的工具/技术

c++ - mbstowcs_s 函数调整大小有时会返回输入长度 + 1

ios - OpenGL ES 纹理质量下降

c++ - 快速 C++ 字符串输出

c - c 处理 12 位数字时的性能问题

c - c中的自由函数

c - 尽管使用 continue,While 循环会无限地卡在 else 语句中

c - 结构功能

c - 让一个简单的 Makefile 工作

c - 初始化和访问struct中的char数组