int main()
{
int (*x)[5]; //pointer to an array of integers
int y[6] = {1,2,3,4,5,6}; //array of integers
int *z; //pointer to integer
z = y;
for(int i=0;i<6;i++)
printf("%d ",z[i]);
x = y;
for(int i=0;i<6;i++)
printf("%d ",(*x)[i]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
以上printfs都打印数字1到6.
如果" 指向整数数组的指针 "和" 指向整数的指针 "都可以做同样的事情,它们是否具有相同的内部表示?
编辑:这个代码确实在编译时发出警告,如下面的答案所指出的,但它确实在我的x86_64机器上使用gcc正确打印值
考虑这个计划
int main()
{
float f = 11.22;
double d = 44.55;
int i,j;
i = f; //cast float to int
j = d; //cast double to int
printf("i = %d, j = %d, f = %d, d = %d", i,j,f,d);
//This prints the following:
// i = 11, j = 44, f = -536870912, d = 1076261027
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么从double/float到int的转换在第一种情况下正常工作,并且在printf中完成时不起作用?
该程序是在32位linux机器上的gcc-4.1.2上编译的.
编辑: Zach的答案似乎是合乎逻辑的,即使用格式说明符来确定从堆栈弹出的内容.但是请考虑这个后续问题:
int main()
{
char c = 'd'; // sizeof c is 1, however sizeof …Run Code Online (Sandbox Code Playgroud) 考虑以下代码:
int main()
{
int e;
prn(e);
return 0;
}
void prn(double x,int t)
{
}
Run Code Online (Sandbox Code Playgroud)
为什么此代码会发出以下警告并且没有错误?
m.c:9: warning: conflicting types for ‘prn’
m.c:5: note: previous implicit declaration of ‘prn’ was here
Run Code Online (Sandbox Code Playgroud)
它不应该给出"未定义函数"错误吗?
考虑以下代码:
void res(int a,int n)
{
printf("%d %d, ",a,n);
}
void main(void)
{
int i;
for(i=0;i<5;i++)
res(i++,i);
//prints 0 1, 2 3, 4 5
for(i=0;i<5;i++)
res(i,i++);
//prints 1 0, 3 2, 5 4
}
Run Code Online (Sandbox Code Playgroud)
查看输出,似乎每次都不会从右到左评估参数.到底发生了什么?
据我所知,在预处理阶段,代码中所有出现的NULL都将替换为0。然后,在编译期间,指针上下文中所有出现的0都将替换为代表该机器上NULL的适当值。因此,编译器必须知道该特定机器的NULL值。
现在,这意味着每当我在指针上下文中使用0时,它将被表示该机器上NULL的适当值替换,该值可以为0,也可以不为0。 ,当我在指针上下文中使用0时?
很抱歉,冗长的描述。如果我错了请纠正我