[英]How to assign a value from an address to a pointer on the go?
Causes a segmentation fault: 导致分段错误:
int n;
int *variable = scanf("%d",&n);
printf("Printing :%d",*variable);
No problem: 没问题:
int n;
scanf("%d",&n);
int *variable = &n;
printf("Printing :%d",*variable);
How to achieve the first one without the segmentation fault? 如何实现没有分割错误的第一个?
Assuming scanf
was successful, it returns 1 (in general it returns the number of variables that were set). 假设
scanf
成功,则返回 1(通常返回设置的变量数)。
In your second snippet you discard that (useful) information, and you are setting a pointer varaible
to the address of n
, which is perfectly valid. 在第二个代码段中,您丢弃了这些(有用的)信息,并且正在设置一个
varaible
的指针,该指针可varaible
n
的地址,这是完全有效的。 (If scanf
returned 0 then n
would be uninitialised, and the behaviour of your printf
call undefined .) (如果
scanf
返回0,则n
将未初始化,并且printf
的行为调用undefined 。)
In the first snippet you set a pointer to the int
constant 0
or 1
depending on the return value of scanf
. 在第一个代码段中,根据
scanf
的返回值,将指针设置为int
常量0
或1
。 That's fine too, but the behaviour on dereferencing that pointer with 也可以,但是使用取消引用该指针的行为
*variable
is undefined . 未定义 。
If you want to be flashy, and have a robust conversation with your code reviewer, you could use the expression separator operator and write 如果您想变得浮华,并与代码审阅者进行健壮的对话,则可以使用表达式分隔符运算符并编写
int n;
int *variable = (scanf("%d",&n), &n);
but that's naughty since, again, you discard the return value of scanf
, and n
could be uninitialised. 但这很顽皮,因为您再次舍弃了
scanf
的返回值,并且n
可能未初始化。
scanf("%d",&n);
returns integer not pointer to integer. 返回整数而不是整数的指针。 Below is the prototype of scanf.
下面是scanf的原型。
int scanf(const char *format, ...);
When you point like below you are actually pointing to invalid address and it will lead to undefined behavior. 当您像下面这样指向时,您实际上是在指向无效地址,这将导致未定义的行为。
int *variable = scanf("%d",&n);
When you declare and initialize the pointer you assign the value to the pointer itself. 在声明和初始化指针时,您将值分配给了指针本身。 The
*
is needed to show the compiler that you are declaring the pointer to the object of some type not the object itself *
需要向编译器显示您声明的是指向某种类型对象的指针,而不是对象本身
When you use the *
later in the code you dereference that pointer. 当稍后在代码中使用
*
,将取消引用该指针。
void foo(void)
{
int a;
int *b = &a; //you assign the pointer `b` itself - no dereferencing
*b = 5; //you dereference the pointer and asssign the the referenced object
}
The "fresh" declared pointer (unless initialized with the reference to the valid object) does not point to the valid object and its dereferencing invokes the Undefined Behavior 声明的“新鲜”指针(除非使用对有效对象的引用进行了初始化)不指向有效对象,并且其取消引用会调用未定义行为
void foo(void)
{
int a;
int *b = &a; //b is pointing to the valid object -> the variable `a`
int *c; //c is not pointing to valid object
int *d; //d is not pointing to valid object
c = malloc(sizeof(*c)); //now c is pointing to the valid object (if malloc has not failed of course)
*c = 5 // correct no UB
*d = 5; // incorrect - UB
*b = 5; //correct no UB
}
the syntax is similar (the *
before the pointer name) but it does something completely different. 语法相似(指针名称前的
*
),但功能完全不同。 I have noticed that it is a bit confusing for the beginners 我注意到对于初学者来说这有点令人困惑
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.