C++ 将 int 值分配给地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17852911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Assigning int value to an address
提问by Ayse
I thought the following codes were correct but it is not working.
我认为以下代码是正确的,但它不起作用。
int x, *ra;
&ra = x;
and
和
int x, ra;
&ra = x;
Please help me if both of these code snippets are correct. If not, what errors do you see in them?
如果这两个代码片段都正确,请帮助我。如果没有,您会在其中看到哪些错误?
回答by Grijesh Chauhan
Your both expressions are incorrect, It should be:
你的两个表达都不正确,应该是:
int x, *ra;
ra = &x; // pointer variable assigning address of x
&
is ampersandis an address of operator (in unary syntax), using &
you can assign address of variable x
into pointervariable ra
.
&
是符号是操作者的地址(在一元语法),使用&
可以分配可变的地址x
到指针变量ra
。
Moreover, as your question title suggests: Assigning int value to an address
.
此外,由于你的问题标题所暗示的:Assigning int value to an address
。
ra
is a pointer contains address of variable x
so you can assign a new value to x
via ra
ra
是一个包含变量地址的指针,x
因此您可以x
通过ra
*ra = 20;
Here *
before pointer variable (in unary syntax) is deference operatorgives value at the address.
这里*
在指针变量之前(以一元语法)是尊重运算符在地址处给出值。
Because you have also tagged question to c++so I think you are confuse with reference variabledeclaration, that is:
因为您还为c++标记了问题,所以我认为您对引用变量声明感到困惑,即:
int x = 10;
int &ra = x; // reference at time of declaration
Accordingly in case of the reference variable, if you want to assign a new value to x
it is very simply in syntax as we do with value variable:
因此,在引用变量的情况下,如果您想为其分配一个新值x
,语法非常简单,就像我们对 value 变量所做的一样:
ra = 20;
(notice even ra
is reference variable we assign to x
without &
or *
still change reflects, this is the benefit of reference variable: simple to use capable as pointers!)
(注意甚至ra
是我们分配给的引用变量x
没有&
或*
仍然改变反映,这是引用变量的好处:简单易用,能够作为指针!)
Remember reference binding given at the time of declaration and it can't change where pointer variable can point to the new variable later in the program.
记住在声明时给出的引用绑定,它不能改变指针变量可以在程序后面指向新变量的位置。
In C we only have pointer and value variables, whereas in C++ we have a pointer, reference and value variables. In my linked answer I tried to explain differences between pointer and reference variable.
在 C 中,我们只有指针和值变量,而在 C++ 中,我们有指针、引用和值变量。在我的链接答案中,我试图解释pointer 和 reference variable 之间的差异。
回答by Jaywalker
Both are incorrect.
两者都不正确。
When you declare a pointer, you assign it the address of a variable. You are attempting the other way round. The correct way would be:
当你声明一个指针时,你为它分配了一个变量的地址。你正在尝试相反的方式。正确的方法是:
int x,*ra;
ra = &x;
回答by Some programmer dude
Both of those causes, in the way you have them in your question, undefined behavior.
这两个原因,就你在问题中提出的方式而言,都是未定义的行为。
For the first, you don't initialize the pointer, meaning it points to a random location (or NULL
if the variable is global).
首先,您不初始化指针,这意味着它指向一个随机位置(或者NULL
如果变量是全局的)。
For the second, you try to change the address the variable is located at, which (if it even would compile) is not allowed.
对于第二个,您尝试更改变量所在的地址,这是不允许的(如果它甚至可以编译)。
回答by moooeeeep
Here's some annotated code:
这是一些带注释的代码:
int main () {
// declare an int variable
int x = 0;
// declare a pointer to an int variable
int *p;
// get the memory address of `x`
// using the address-of operator
&x;
// let `p` point to `x` by assigning the address of `x` to `p`
p = &x;
// assign `x` a value directly
x = 42;
// assign `x` a value indirectly via `p`
// using the dereference operator
*p = 0;
// get the value of `x` directly
x;
// get the value of `x` indirectly via `p`
// using the dereference operator
*p;
}
Note that dereferencing a pointer that doesn't point to a valid object of the specified type is not allowed.
请注意,不允许取消引用不指向指定类型的有效对象的指针。
So you normally shouldn't do things like the following (unless you really know what you are doing):
所以你通常不应该做以下事情(除非你真的知道你在做什么):
*(int*)(12345) = 42; // assign an integer value to an arbitrary memory address
回答by mathk
Here is my 2 cent.
这是我的 2 美分。
If you are going onto understanding pointer in C. First make the distinction between *
the operator and *
the type qualifier/specifier.
如果你打算在 C 中理解指针。首先区分*
运算符和*
类型限定符/说明符。
See that in C *
is a syntaxique element that can plays both role but never at the same time. A type qualifier:
请注意,在 C 中*
是一个语法元素,它可以同时扮演两种角色,但不能同时扮演。类型限定符:
int a;
int * c = &a;
int * my_function_returning_pointer();
And for getting the proper int. As an operator. ( *c
is an alias of a
)
并获得适当的int。作为运营商。(*c
是 的别名a
)
*c = 9;
I admit that is quite confusing and can trap a lot of beginner. Make sure that you recognize when *
is used as an operator or when it is used as a type qualifier.
我承认这很令人困惑,并且会困住很多初学者。确保您识别何时*
用作运算符或何时用作类型限定符。
The same things apply to &
although it is less often used as type qualifier.
&
尽管它不常用作类型限定符,但同样的事情也适用。
int & f = returning_a_reference();
int my_function( int & refParam);
It is more often use for getting the address of an object. Thus it is used as an operator.
它更常用于获取对象的地址。因此它被用作运算符。
c = &f;
回答by Srinivas Thanneeru
case 1:
int x,*ra;
&ra = x;
it is wrong in c, because in c we can point to a memory location by using a pointer ( i.e *ra in your case ). this can be done as fallows
int x, *ra; x ---------
ra=&x; ra --->1000 | value |
----------
NOTE : with out initializing a variable we can't use pointer to hold the address of that variable, so you better to first initialize variable, then set pointer to that memory location.
int x, *ra;
x=7;
ra=&x;
Fallowing may be helpful to you:
problems(mistake we do ) in handling pointers :
1.)
int a ,*p;
p=a; // assigning value instead of address. that leads to segmentation fault at run time.
2)
int a, *p;
&p=a; // look at here wrong assignment
3)
char *p="srinivas"
strcat(p, "helo" ) ; // we cant add a substring to the constant string.
4) int a=5, *p;
p=5; // the pointer here will points to location 5 in the memory, that may damage whole system, be care full in these type of assignments.