使用void指针取消引用float变量:
#include <stdio.h>
int main() {
float a = 7.5;
void *vp = &a;
printf("%f", *(float*)vp); /* Typecasting a void pointer to float for dereference */
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
输出: 7.500000
使用整数指针取消引用变量:
#include <stdio.h>
int main() {
float a = 7.5;
int *ip = &a;
printf("%f", *(float*)ip); /* Typecasting an int pointer to float for dereference */
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
输出: 7.500000
在两者中,输出都是相同的.当我们能够通过类型转换普通指针来解决不同的数据类型变量时,是什么原因?
用于打印char的十进制值的程序:
#include<stdio.h>
int main(void){
char ch = 'AB';
printf("ch is %d\n",ch);
}
Run Code Online (Sandbox Code Playgroud)
为什么打印第二个字符的十进制值,为什么不是第一个字符的十进制值?
输出: ch is 66
/**** Program to find the sizeof string literal ****/
#include<stdio.h>
int main(void)
{
printf("%d\n",sizeof("a"));
/***The string literal here consist of a character and null character,
so in memory the ascii values of both the characters (0 and 97) will be
stored respectively which are found to be in integer datatype each
occupying 4 bytes. why is the compiler returning me 2 bytes instead of 8 bytes?***/
return 0;
}
Run Code Online (Sandbox Code Playgroud)
输出:
2
Run Code Online (Sandbox Code Playgroud) c sizeof type-conversion string-literals implicit-conversion