-3

According to these links: stackoverflow question and C++ FQA references cannot refer to another object/ variable well once they're initialized, but what about the below code?

// Example program in C++
#include <iostream>
using namespace std;
int main()
{
    int x=10, z=12;
    int &y=x;
    ++y;
    y=z; //valid?
    cout << x << ", "<< y <<", " << z << endl; // prints 12, 12, 12
    return 0;
}

Below is the C code regarding pointer reseating and it seems valid, am I right?

#include <stdio.h>
int main(int argc, char *argv[])
{
    int a=10, b=20;
    int *ptr;   
    ptr = &a;
    ptr = &b;
    printf("%d\n",a); // prints 10
    printf("%d\n",*ptr); // prints 20
    return 0;
}

Can someone clear the above concept in the above two codes?

Community
  • 1
  • 1
highlander141
  • 1,683
  • 3
  • 23
  • 48
  • Your code says it all. `y=z;` changed `x`. – chris Oct 31 '15 at 17:59
  • Yes. Reseating pointers is fine, reseating references is not. – Martin James Oct 31 '15 at 17:59
  • @chris thanks for the comment, but then what is reference reseating? is it not reference reseating from x to z !? – highlander141 Oct 31 '15 at 18:01
  • Please choose one of C and C++. C doesn't have references, so I tagged this as C++. Please retag if your intent was different. – fuz Oct 31 '15 at 18:04
  • 1
    @highlander141, C++ has no legal reference reseating. The closest thing is your pointer example, which is what `std::reference_wrapper` does internally when you reseat it. – chris Oct 31 '15 at 18:11

2 Answers2

3
y=z; //valid?

Absolutely! However, it does not mean "y refers to z from now on". It means "set the value of z to whatever y is currently referring", which is x. Hence, y=z is another way of writing x=z.

Below is the C code regarding pointer reseating and it seems valid, am I right?

Unlike references, pointers can be re-pointed, so the re-assignment of the pointer makes it point to a different variable. Your program does not illustrate this, however, because two assignments to ptr happen without any reads of ptr in between, so only the second assignment stays.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • thank you for your answer, what is invalid expression or atleast how we try to reseat reference and I guess we get compile time error huh? – highlander141 Oct 31 '15 at 18:14
  • 1
    @highlander141 The expression is valid, it's just that it does not change the value of the reference; it changes the value of whatever the reference is referring to. With pointers you have two syntaxes: one for changing the pointer (without the asterisk) and one for changing whatever the pointer points to (with the asterisk). There is no syntax for re-setting the reference: no asterisk means "change whatever the reference is referring to", and asterisk is a syntax error (unless the reference refers to a pointer, but that's an unrelated story altogether). – Sergey Kalinichenko Oct 31 '15 at 18:17
2

In y=z

Its not refering to another variable its just assigning the value of z to y which is 12 and since y is reference to x, x also gets assigned value 12.

so x=y=z=12

But in pointers its valid to change the address it points to:

ptr = &a;
ptr = &b; //valid
wrangler
  • 3,454
  • 1
  • 19
  • 28
  • thank you for your answer, so then what is 'invalid expression' regarding reference in C++ – highlander141 Oct 31 '15 at 18:11
  • 1
    @highlander141 No matter how may times you assign value to reference variable its completely valid as its just overwriting value of variable but its just that you cant reset reference as its invalid in c++. – wrangler Oct 31 '15 at 18:27