Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How can a referece to integer constant change value in a function?

How can a referece to integer constant change value in a function?

Scheduled Pinned Locked Moved C / C++ / MFC
question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 3971400
    wrote on last edited by
    #1

    Could someone pls explain the below question? Q1 : In line1 the second parameter is referece to integer constant. If so how is the value in 'x' changed to 4 Question:

    #include

    #include

    using namespace std;

    int mani(int (&arr)[10], const int& x)//Line1
    {
    for (int i = 0; i < 1; ++i)
    arr[i] = arr[i] + x;
    return x; //returns 4
    }

    int main(){
    int arr[10] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
    cout<

    N C 2 Replies Last reply
    0
    • U User 3971400

      Could someone pls explain the below question? Q1 : In line1 the second parameter is referece to integer constant. If so how is the value in 'x' changed to 4 Question:

      #include

      #include

      using namespace std;

      int mani(int (&arr)[10], const int& x)//Line1
      {
      for (int i = 0; i < 1; ++i)
      arr[i] = arr[i] + x;
      return x; //returns 4
      }

      int main(){
      int arr[10] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
      cout<

      N Offline
      N Offline
      Naveen
      wrote on last edited by
      #2

      Member 3974347 wrote:

      If so how is the value in 'x' changed to 4

      Since the variable x is a reference to the a[0], when ever you use x, keep in mind that you are actually using a[0] itself. And in the for loop, when the value of i = 0, you chnages the value of a[0] and so the x will also points to new value. Defining the x as constant only prevents directly chaging the value of x, using the x variable itself. something like x = a[2]; will be shown as error.

      nave [OpenedFileFinder] [My Blog]

      1 Reply Last reply
      0
      • U User 3971400

        Could someone pls explain the below question? Q1 : In line1 the second parameter is referece to integer constant. If so how is the value in 'x' changed to 4 Question:

        #include

        #include

        using namespace std;

        int mani(int (&arr)[10], const int& x)//Line1
        {
        for (int i = 0; i < 1; ++i)
        arr[i] = arr[i] + x;
        return x; //returns 4
        }

        int main(){
        int arr[10] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
        cout<

        C Offline
        C Offline
        clayman87
        wrote on last edited by
        #3

        Well, "x" an indeed an "integer constant" from the our point of view: by taking a constant reference, we obligate ourselves not to change the value of "x", we can only read it. However, it does not mean that the value of x has to be constant, it's just that _we_ can't modify the respective memory area through this variable named "x". If it's not a constant actually, then anyone else, or even us, through an other alias can modify the memory area (in this example through the non-const reference to the array), the modified value of course reflecting also when accessing the value of x. So, in this example, "x" is just an alias to the first element of the "arr" array marked const (=you can't modify the value through me), but since the function takes the array by reference (thereby obtaining an alias for the original "arr" array) and changes its first element (remember, the array is a non-const reference, it can be changed), it actually changes the first element of the original, global "arr" array, but "x" is still just an alias for this, thereby its value must be 2 + 2 = 4.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups