1

The function get of an ifstream reads the next character and stores it in the argument you pass to the function. Example program:

#include <iostream>     
#include <fstream>      

int main () {
  std::ifstream is("input.txt");     // open file
  char c;

  while (is.get(c))          // loop getting single characters
    std::cout << c;

  is.close();                // close file
  return 0;
 }

This works great but I am puzzled by how c can be changed by the function get as it is not passed by its pointer. I was told, some time ago, that modifying a variable within a function cannot change its value outside the function. And that's the whole purpose of pointers, right -- manipulating an object created outside the function. So how can c be changed here?

I guess there is something obvious that I don't understand here?

Godzy
  • 157
  • 1
  • 10
  • in C++, there are references. that's how. if you pass a parameter by non-`const` reference, you can modify it. (in essence, most compilers will generate identical machine code as if the object was passed by-pointer.) – The Paramagnetic Croissant Mar 25 '15 at 19:03
  • 2
    I'm voting to close this question as off-topic because it lacks basic knowledge about the language being used. – The Paramagnetic Croissant Mar 25 '15 at 19:04
  • 3
    At the least, you could look at the signature of `get`. – chris Mar 25 '15 at 19:06
  • I did not know of references. I only learned C, and I've only recently transfered to C++ without any formal training. When I see "&", it means memory address to me... What is the signature? – Godzy Mar 25 '15 at 19:24
  • @TheParamagneticCroissant: I think that's a poor close excuse. Beginners should feel free to ask questions, including simple ones. Now, if you want to vote to close because you don't think the OP has done "due diligence" in trying to find ananswer: sure, go ahead. But saying someone can't ask a question because they lack knowledge is kinda weird... the whole point of asking questions is to gain new knowledge (whether basic or not). SO doesn't say it requires askers to meet some arbitrary threshold of knowledge. – Cornstalks Mar 25 '15 at 19:24
  • @Cornstalks Stack Overflow is not a language tutorial site. Consequently, it should not be used as such. – The Paramagnetic Croissant Mar 25 '15 at 19:26
  • @Godzy: If you [google "ifstream get"](https://www.google.com/search?sourceid=chrome-psyapi2&rlz=1C5CHFA_enUS557US557&ion=1&espv=2&ie=UTF-8&q=ifstream%20get&oq=ifstream%20get&aqs=chrome..69i57j0l5.3464j0j7) and click one of the top two results, it will show you the function's signature... – Cornstalks Mar 25 '15 at 19:26
  • @TheParamagneticCroissant: Why not? Where do the SO guidelines/rules say that? [This question is totally valid for SO](http://stackoverflow.com/questions/2765999/what-is-a-reference-variable-in-c) (which this question seems to be a duplicate of, IMO). – Cornstalks Mar 25 '15 at 19:28
  • 1
    @TheParamagneticCroissant Yes I'm a noob. I did my research, but sometimes being a noob means you don't know what you're looking for. I went to the documentation of the function and, because I saw the & character assumed I was supposed to pass some sort of pointer but it would only work when I passed the variable and I was puzzled. Now I know i have to learn about "references", the answer is obvious. Should I use a different forum to ask my noob questions? I thought stack overflow was also for noobs like me, trying to politely ask questions to people who have the knowledge. Sorry! – Godzy Mar 25 '15 at 19:36

2 Answers2

2

The member function std::istream::get() uses a reference:

istream& get (char& c);

This means that the function accesses directly to the variable passed as a parameter.

If you're not familiar with references, here you can learn more.

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • Thank you very much for the link to references! That's the obvious thing I did not know. – Godzy Mar 25 '15 at 19:26
1

"So how can c be changed here?"

As from the reference documentation std::ifstream::get() uses a parameter passed by reference for the char variable

 basic_istream& get( char_type& ch );

so it alters it by using this reference.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190