-8
  1. Is int &y=x same as int y=&x?

  2. Are s++ and *s++ the same?

  3. Also in the below code, why is *s++ giving me some wrong results?

    I was expecting *s value to be 12

#include <iostream>
using namespace std;

int main()
{
    int p=10;
    int &q=p;   //q is a reference variable to p
    //int r=&p; //error: invalid conversion from 'int*' to 'int'
    int *s=&p;  //valid
    q++;        
    *s++;       //here even s++ works, and cout<<*s does not give 12 but some lengthy number
                //and cout<<s gives some hexadecimal, I'm guessing thats the address
    cout<<p<<endl<<q<<endl<<*s;
}

Output I'm getting:

11
11
6422280
vard
  • 4,057
  • 2
  • 26
  • 46
Abhilash Kishore
  • 2,063
  • 2
  • 15
  • 31
  • 1
    The reason that `r=&p` gave you an error is because you were declaring an `int`, meaning you were telling the compiler, "hey, I'm gonna store an integer in memory." But instead of handing it an integer, you handed it an address where an integer was stored. It's a type mismatch. The compiler attempted to convert it to an int implicitly, but addresses looks nothing like ints. Hence the error. – Isaiah Taylor Jan 05 '16 at 05:04
  • 1
    These are peculiar questions to ask. 1. Since one is an error and the other isn't, they can't possibly be the same. 2. Since they don't have the same effect, they can't possibly be the same. – molbdnilo Jan 05 '16 at 07:39

3 Answers3

3

Is int &y=x same as int y=&x?

No. They are very different.

int x;
int& y = x; // y is a reference to x.

On the other hand,

int x;
int y = &x; // Should be compiler error.
            // Initializing y with a pointer.

The line:

*s++;

is equivalent to:

int* temp = s;
s++;
*temp;

You are evaluating *temp but the side effect is that you are incrementing s. After that s points to the next element. That is not a valid address though. The next time you access *s, the program exhibits undefined behavior. The output of the line:

cout<<p<<endl<<q<<endl<<*s;

can be anything.

R Sahu
  • 204,454
  • 14
  • 159
  • 270
3

int &y=x same as int y=&x.

In first y is an integer reference which is initialized to x . OTOH, in second you are trying to assign int a value of int * which results in a compilation error .

why is *s++ giving me some wrong results? I was expecting *s value to be 12

This is because ++ has precedence over * , therefore ,first s is incremented first (thus pointing to uninitialized memory location), and then dereferenced .

Which is leading to dereference of uninitialized memory location resulting in undefined behaviour.

If you want to expected value , you can do this -

(*s)++;         //dereference first and then increment
ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • (*s)++ fixed it. Thanks for telling me about the precedence of ++. what about 's++' ? That is not valid? What does it do? – Abhilash Kishore Jan 05 '16 at 06:44
  • @Abhilashk `s++` would point to location past the memory address of variable `p` . This address may be invalid or uninitialized , so dereference causes undefined behaviour. But if you do `s++` and not dereference it and print address , then there should be no problem IMHO . – ameyCU Jan 05 '16 at 06:53
  • "But if you do s++ and not dereference it and print address , then there should be no problem IMHO" can you please show it programatically and the quote the expected output? – Abhilash Kishore Jan 05 '16 at 07:09
  • @Abhilashk Its just `s++;` and then print `s` and expected output would be address of `p` plus `sizeof(int)`. – ameyCU Jan 05 '16 at 07:13
1

Is int &y=x same as int int y=&x?

Not nearly. The first binds the x to the int-reference y, the second initializes the int y with the address of x.

Both can be made to compile cleanly, but the second only with severe contortions abusing operator overloading:

{
    int y;
    int& x = y;
}
{
    // Only for demonstration, never do something this crazy for real
    struct { int operator&() { return 0; }; } x;
    int y = &x;
}

Are s++ and *s++ the same?

Certainly not, though they have the same effect in your example, as you discard the result:

  • The first increments s and returns its pre-increment value.
  • The second does the same, but then dereferences that.

In both cases, s thereafter points behind an object, and may not be dereferenced on pain of Undefined Behavior (UB).
Which explains the curious number you got, but might also have resulted in your program just printing 42 and then beginning to reformat your harddrive (legal but unlikely).

Community
  • 1
  • 1
Deduplicator
  • 44,692
  • 7
  • 66
  • 118