关于c点,看不懂输出
about c point, can‘t understand the output
#include<stdio.h>
typedef unsigned char * char_point;
char_point int_cp(int i)
{
printf("i: %d\n", i);
printf("&i: %p\n", &i);
printf("(c_p)&i: %p\n", (char_point) &i);
char_point cp = (char_point) &i;
return cp;
}
char_point float_cp(float f)
{
printf("f: %f\n", f);
printf("&f: %p\n", &f);
printf("(c_p)&f: %p\n", (char_point) &f);
char_point cp = (char_point) &f;
return cp;
}
void endian(char_point cp, int size)
{
for (int i=0; i<size; i++)
{
printf("0x%x\n", cp[i]);
}
}
int main()
{
int i = 0x21893678;
float f = 913.45678f;
endian(int_cp(i), sizeof(i));
// endian((char_point) &i, sizeof(i)); except like this
printf("== == == === = ==== === = == = == \n");
// endian((char_point) &f, sizeof(f));
endian(float_cp(f), sizeof(f));
}
结果:
i: 562640504
&i: 0x7ffdddb42fac
(c_p)&i: 0x7ffdddb42fac
0xfd
0x7f
0x0
0x0
== == == === = ==== === = == = ==
f: 913.456787
&f: 0x7ffdddb42fac
(c_p)&f: 0x7ffdddb42fac
0xfd
0x7f
0x0
0x0
异常:
this returns 指向本地 i:
的指针
char_point cp = (char_point) &i;
和指向本地 f:
的指针
char_point cp = (char_point) &f;
一旦函数returns,它们就会被释放
#include<stdio.h>
typedef unsigned char * char_point;
char_point int_cp(int i)
{
printf("i: %d\n", i);
printf("&i: %p\n", &i);
printf("(c_p)&i: %p\n", (char_point) &i);
char_point cp = (char_point) &i;
return cp;
}
char_point float_cp(float f)
{
printf("f: %f\n", f);
printf("&f: %p\n", &f);
printf("(c_p)&f: %p\n", (char_point) &f);
char_point cp = (char_point) &f;
return cp;
}
void endian(char_point cp, int size)
{
for (int i=0; i<size; i++)
{
printf("0x%x\n", cp[i]);
}
}
int main()
{
int i = 0x21893678;
float f = 913.45678f;
endian(int_cp(i), sizeof(i));
// endian((char_point) &i, sizeof(i)); except like this
printf("== == == === = ==== === = == = == \n");
// endian((char_point) &f, sizeof(f));
endian(float_cp(f), sizeof(f));
}
结果:
i: 562640504
&i: 0x7ffdddb42fac
(c_p)&i: 0x7ffdddb42fac
0xfd
0x7f
0x0
0x0
== == == === = ==== === = == = ==
f: 913.456787
&f: 0x7ffdddb42fac
(c_p)&f: 0x7ffdddb42fac
0xfd
0x7f
0x0
0x0
异常:
this returns 指向本地 i:
的指针char_point cp = (char_point) &i;
和指向本地 f:
的指针char_point cp = (char_point) &f;
一旦函数returns,它们就会被释放