没有较为混乱指针 [英] No More Confusing Pointers
问题描述
以下是我的code:
的#include<&stdio.h中GT;
诠释的main()
{
所以char a [10] =你好!;
无效F(焦B〔])
{
// 1的printf(* a为:%S \\ n,*一);
的printf(&放大器;一为:%p \\ n,&安培; A);
// 2的printf(与&&放大器;一个为:%p \\ N,及(&放大器;一));
输出(A的地址为:%p \\ n,一);
的printf(a的值是:%S \\ n,一);
的printf(B的地址为:%p \\ n,B);
的printf(B的值是:%S \\ n,B);
// 3的printf(* b为:%S \\ n,* B);
输出(和b为:%S \\ n,和b);
} F(一); 返回1;
残培();
}
运行上面的code给出的输出:
&放大器;一个是:0028FF1C
a的地址是:0028FF1C
a的值是:嗨!
B的地址是:0028FF1C
B的值是:嗨!
和b是:∟(
在输出:
为什么是&放有不同的输出;一个
和和b
;
尽管a和b有相同的参考
另外,
我提到他们的3号意见。
如果我删除斜线逐一执行它们,我获得以下2个问题:
-
在执行任何评论。 1和; 3:
abc.exe已停止工作。
-
在执行任何评论。 2:
abc.c:在函数'F':
abc.c:14:32:错误:需要左值的一元'和;'操作
的printf(与&&放大器;一个为:%p \\ N,及(&放大器;一));
^
-
第一点: 嵌套函数不是标准的
C
。他们也支持GCC扩展。 。 -
2点:
的printf(和b为:%S \\ n,和b);
是错的并调用UB,因为不正确的格式说明的。您需要更改为的printf(和b为:%p \\ n,(无效*)和b);
-
3点:
及(&安培; A)
是错误的。操作数为&安培;
必须是一个左值,而不是其他的地址的,这是不是一个左值。相关阅读:
C11
,章§6.5.3.2
,对于操作数类型
的操作数一元
&安培;
运营商应是一个功能标志,结果的[]
或者一元*
运营商,或者指定的对象不是一个位域,不使用寄存器存储类说明符声明。左值
块引用>和返回类型
......结果不是一个左值。
块引用>
Following is my code:
#include <stdio.h>
int main()
{
char a[10]="Hi!";
void f(char b[])
{
// 1. printf("*a is:%s\n",*a);
printf("&a is:%p\n",&a);
// 2. printf("&&a is:%p\n",&(&a));
printf("a's address is:%p\n",a);
printf("a's value is:%s\n",a);
printf("b's address is:%p\n",b);
printf("b's value is:%s\n",b);
// 3. printf("*b is:%s\n",*b);
printf("&b is:%s\n",&b);
}
f(a);
return 1;
getch();
}
Running the above code gives the output:
&a is:0028FF1C
a's address is:0028FF1C
a's value is:Hi!
b's address is:0028FF1C
b's value is:Hi!
&b is:∟ (
In the Output:
Why are there different outputs for &a
and &b
;
Although a and b have same reference.
Further,
I've mentioned 3 comments by their number. If I remove slashes one by one and execute them,I get following 2 issues:
On executing comment no. 1 & 3:
"abc.exe has stopped working."
On executing comment no. 2:
abc.c: In function 'f': abc.c:14:32: error: lvalue required as unary '&' operand printf("&&a is:%p\n",&(&a)); ^
Point 1: Nested functions are not standard
C
. They are supported as GCC extension..Point 2:
printf("&b is:%s\n",&b);
is wrong and invokes UB, because of improper format specifier. You need to change that toprintf("&b is:%p\n",(void *)&b);
Point 3:
&(&a)
is wrong. the operand for&
needs to be an lvalue, not another address, which is not an lvalue.Related:
C11
, chapter§6.5.3.2
, for the operand typeThe operand of the unary
&
operator shall be either a function designator, the result of a[]
or unary*
operator, or an lvalue that designates an object that is not a bit-field and is not declared with the register storage-class specifier.and the return type
....result is not an lvalue.
这篇关于没有较为混乱指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!