char *ptr = malloc(sizeof(char));
printf("\nValue of ptr: %d\n",ptr);
printf("\nValue of address of ptr: %x\n",&ptr);
printf("\nValue of pointed by ptr: %c\n",*ptr);
printf("\nValue of address pointed by ptr: %x\n",&(*ptr));
*ptr='u';
printf("\nValue of pointed by ptr: %c\n",*ptr);
printf("\nValue of address pointed by ptr: %x\n",&(*ptr));
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,收到以下输出:
Value of ptr: 1046113600
Value of address of ptr: a2fffa78
Value of pointed by ptr: P
Value of address pointed by ptr: 3e5a6d40
Value of pointed by ptr: u
Value of address pointed by ptr: 3e5a6d40
Run Code Online (Sandbox Code Playgroud)
这是否意味着具有单个字符大小的malloc的默认值为P?
尝试使用 …
我正在尝试分析以下代码:
FILE* out=stdout;
printf("\nEnter value: \n");
out=out-1;
char ch;
while(ch!=EOF){
ch=fgetc(out);
printf("\nValue printed: %c\n",ch);
}
fclose(out);
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,会观察到以下输出:
Enter value:
IWantToC
Value printed: I
Value printed: W
Value printed: a
Value printed: n
Value printed: t
Value printed: T
Value printed: o
Value printed: C
Value printed:
Run Code Online (Sandbox Code Playgroud)
我知道 fgetc 一次从 out(指向 stdout)获取一个字符的值并打印它直到到达文件末尾。
out=out-1 在这里做什么?如果 out 最初指向 stdout 文件的开头,那么它不应该也打印“Enter value:”中的字符。或者,如果它指向 stdout 文件的末尾,那么它不应该只从末尾打印一个字符吗?另外,为什么它首先需要用户输入?是因为getc吗?
抱歉问了很多问题。如果有人澄清这些疑问,这对我理解 C 将会非常有帮助。我在这里看到了相关问题,但没有找到任何特别解决我的疑问的问题。