3

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?

afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
Ayse
  • 2,676
  • 10
  • 36
  • 61

6 Answers6

20

Both of your expressions are incorrect, It should be:

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

& is ampersand is an address of operator (in unary syntax), using & you can assign address of variable x into pointer variable ra.

Moreover, as your question title suggests: 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 = 20; 

Here, the * before pointer variable (in unary syntax) is the deference operator which gives the value stored at the address.

Because you have also tagged question to so I think you are confuse with reference variable declaration, that is:

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:

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!)

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.

rocksNwaves
  • 5,331
  • 4
  • 38
  • 77
Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
5

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;
Jaywalker
  • 3,079
  • 3
  • 28
  • 44
5

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).

For the second, you try to change the address the variable is located at, which (if it even would compile) is not allowed.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
3

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
moooeeeep
  • 31,622
  • 22
  • 98
  • 187
1

Here is my 2 cent.

If you are going onto understanding pointer in C. First make the distinction between * the operator and * the type qualifier/specifier.

See that in C * is a syntaxique element that can plays both role but never at the same time. A type qualifier:

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)

*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;
mathk
  • 7,973
  • 6
  • 45
  • 74
0
  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.
Srinivas Thanneeru
  • 161
  • 1
  • 2
  • 12