【问题标题】:Assigning int value to an address将 int 值分配给地址
【发布时间】:2013-07-25 08:33:03
【问题描述】:

我认为以下代码是正确的,但它不起作用。

int x, *ra;     
&ra  = x;

和

int x, ra;
&ra = x;

如果这两个代码 sn-ps 都正确,请帮助我。如果没有,您在其中看到了什么错误?

【问题讨论】:

  • 你希望代码首先做什么?
  • 只是尝试学习指针,然后尝试不同的事情。

标签: c++ c visual-studio-2010 pointers


【解决方案1】:

你的两个表述都不正确,应该是:

int x, *ra;
ra  = &x;  // pointer variable assigning address of x

& 是ampersand 是运算符的地址(在一元语法中),使用& 可以将变量x 的地址分配给pointer 变量ra。

此外,正如您的问题标题所示:Assigning int value to an address。

ra 是一个指针,包含变量x 的地址,因此您可以通过ra 为x 分配一个新值

*ra = 20; 

这里* 在指针变量(在一元语法中)之前是deference operator 在地址处给出值。

因为您也将问题标记为,所以我认为您对reference variable 声明感到困惑,即:

int x = 10;
int &ra = x; // reference at time of declaration 

因此,在引用变量的情况下,如果你想为x 分配一个新值,它的语法非常简单,就像我们对值变量所做的那样:

ra = 20;

(请注意,即使ra 是我们分配给x 的引用变量,而没有& 或* 仍然会发生变化,这就是引用变量的好处:易于用作指针!)

请记住在声明时给出的引用绑定,它不能改变指针变量在程序后面可以指向新变量的位置。

在 C 中,我们只有指针和值变量,而在 C++ 中,我们有指针、引用和值变量。在我的链接答案中,我试图解释differences between pointer and reference variable。

【讨论】:

    【解决方案2】:

    两者都不正确。

    当你声明一个指针时,你给它分配了一个变量的地址。你正在尝试相反的方式。正确的做法是:

    int x,*ra;
    ra  = &x;
    

    【讨论】:

      【解决方案3】:

      这两个原因,在你的问题中,未定义的行为。

      首先,您不初始化指针,这意味着它指向一个随机位置(或NULL,如果变量是全局的)。

      对于第二个,您尝试更改变量所在的地址,这(如果它甚至可以编译)是不允许的。

      【讨论】:

        【解决方案4】:

        这是一些带注释的代码:

        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;
        }
        

        请注意,不允许取消引用不指向指定类型的有效对象的指针。

        所以你通常不应该做以下事情(除非你真的知道自己在做什么):

        *(int*)(12345) = 42; // assign an integer value to an arbitrary memory address
        

        【讨论】:

          【解决方案5】:

          这是我的 2 美分。

          如果您要了解 C 中的指针。首先要区分 * 运算符和 * 类型限定符/说明符。

          看到在 C 中* 是一个语法元素,它可以同时扮演两个角色,但不能同时扮演。类型限定符:

          int a;
          int * c = &a;
          int * my_function_returning_pointer();
          

          为了获得正确的 int。作为运营商。 (*c 是a 的别名)

          *c = 9;
          

          我承认这很令人困惑,可能会让很多初学者陷入困境。确保您识别 * 何时用作运算符或何时用作类型限定符。

          同样的事情也适用于&,尽管它很少用作类型限定符。

          int & f = returning_a_reference();
          int my_function( int & refParam);
          

          它更常用于获取对象的地址。因此它被用作运算符。

          c = &f;
          

          【讨论】:

            【解决方案6】:
              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.
            

            【讨论】:

              猜你喜欢
              • 2013-11-23
              • 1970-01-01
              • 2014-10-10
              • 2021-12-17
              • 1970-01-01
              • 2013-11-07
              • 2013-10-15
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多