• Advertisement
Sign in to follow this  

reference pointers

This topic is 4178 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Do reference variables work with pointers? My testing is leading me to believe they don't, but I see no reason why C++ should be this way, so maybe I have made an error someone can point out. Intended behavior is for func1 to opens and read a file. Whenever a certain marker is reached, func1 passes to func2 to read a formatted section, then return to func1 once it finishes. I have simplified out some of the irrelevant code to focus on the file io (which was not simplified or modified for this post). Here is a code snippet:
while(1)
{
	//....
	if(EOF == fscanf(pFile, "%s", cBuf)) break;
	if(Initialized >= 0x3F) break;

	if(strcmp(cBuf, "MAP"))
	{
		m_pPlane = new CPlane(rows, cols, pFile);
		if(EOF != fscanf(pFile, "%*s"))
		Initialized |= 0x02;
	}
	//....
}
CPlane::CPlane(int rows, int cols, FILE*& pFile)
{
	//....
	m_fpTU = new float[m_wRows*m_wCols];
	m_fpTV = new float[m_wRows*m_wCols];

	fgetc(pFile);
	for(int i=0; i<m_wCols; i++)
	{
		for(int j=0; j<m_Rows; j++)
		{
			m_fpTU[i*m_wCols+j] = (float)(fgetc(pFile)-'0')*m_fpTileRowsInv;
			m_fpTV[i*m_wCols+j] = (float)(fgetc(pFile)-'0')*m_fpTileColsInv;
			fgetc(pFile);
		}
	}
	//....
}
Problem is that the pFile pointer is in the same place before the constructor for m_pPlane as it is after. edit: fixed html tags

Share this post


Link to post
Share on other sites
Advertisement
You can have a reference to a pointer, but not a pointer to a reference.

Note, however, that C file manipulation functions do not generally affect the pointer itself, but the FILE structure it points to, so passing a reference is probably not necessary here.

Since you're using C++, you could probably use IOStreams instead of C stdio.

Share this post


Link to post
Share on other sites
0) The reason you see what you do is as Fruny said: fgetc() doesn't actually change the pointer, but instead the pointed-at thing.

1) Like Fruny hinted at: you do know we ditched all those ugly (tending towards horrible names and lack of type-safety) file I/O functions years and years ago in favour of something much cleaner, yes? :) Not to mention that reading "strings" into a character buffer (the double quotes are not to suggest string literals, but to indicate that I use the term quite loosely) is a horrible idea if you don't want to deal with, you know, bad data crashing your program before you can get a chance to report the problem.


#include <iostream>
#include <string>
#include <vector>
//...
std::string word;
ifstream file("name of file goes here.txt");
//...
while(file >> word && Initialized >= 0x3F) {
// I can only imagine what kind of horrible stuff you do with that
// "Initialized" variable :s

if (word == "MAP") { // yes, this works!
m_pPlane = new CPlane(rows, cols, file);
// uh, I can't tell what the other bit was supposed to be doing?
}
//....
}

CPlane::CPlane(int rows, int cols, istream& file) :
m_TU(m_wRows * m_wCols), m_TV(m_wRows * m_wCols) {
//....
// Oh, we also have these lovely things now that make your life
// a lot easier in terms of memory management, called vectors.
// Oh, and we have these things called initializer lists that let you
// actually *initialize* members instead of assigning to them in the
// constructor body. That means, for example, that you can use them to
// specify what constructor to use for data members and bases.
// Here I would make m_TU and m_TV each be a vector<float>, and you're
// off to the races.

file.get();
const int size = m_wCols * m_Rows;

for(int i=0; i<size; i++) {
m_TU[i] = (float)(file.get()-'0')*m_fpTileRowsInv;
m_TV[i] = (float)(file.get()-'0')*m_fpTileColsInv;
file.get();
}
//....
}

Share this post


Link to post
Share on other sites
Ok thanks guys, I solved it now :)

About iostreams, are they really that much better? I used those for a while until I discovered fscanf, which is freaking awesome! (for reading formatted files, that is) Iostreams just don't seem to have the coolness of fscanf.

edit: that one fscanf call that seemed to have no purpose was due to the way I wrote the file. Each section begins with a header describing that itself like "MAP" and then ends with "END". That one reads the "END" and discards it.

Share this post


Link to post
Share on other sites
Quote:
Original post by polaris2013
About iostreams, are they really that much better?


If you learn how to use them effectively, yes, they're better. If anything, they're safer, extensible, and better integrated with the rest of the C++ standard library than *scanf() can ever claim to be (it's a C function, after all).

Granted, some of the weirder things you can do with scanf are a bit harder to achieve with iostreams, but that's why there is

[boost]
.

Share this post


Link to post
Share on other sites
Could you describe in words what the "Initialized" variable represents? It looks like you're using it as some kind of bit-field, with every bit having some magical and un-documented meaning. This is bound to make things harder for yourself.

And yes, you can do lots of very nice things with iostreams - but most importantly, they are type-safe: if you change the type of a variable that you're reading into, you don't have to change the code, unless you're changing to a type that can't yet be read - and then you get a compiler error instead of a runtime error. With scanf magic, things can blow up at run-time in any number of bizarre and frightening ways if your magic formatting string doesn't exactly match the arguments you want to read into. Plus, %s reads blindly into memory sequentially following some pointer-to-char, instead of into, you know, strings.

What exactly are you trying to do with a read of "%*s", with no additional arguments specified?

Share this post


Link to post
Share on other sites
Okay Zahlman! I already solved the problem but I'll go ahead and explain further.

The file that I am reading looks like this:

HEADER
//header info
END

MAP
//map info
END

SPRITES
//sprite info
END

//etc

There are 6 of these sections, so there are 6 bits in the Initialized variable that get set (hence 0x3F). All that I use that variable for is as a return value to write to a log and as a break condition from the loop. If it does not reach it's maximum value (0x3F) The program closes itself and writes to a log "Initialization error: " and then the initialization variable. It's basically just for debugging.

And that one %*s reads the "END" at each section. Yes, I know it could actually say "y = mx + b" or something crazy instead of "END", but right now I'm just testing the internal functionality of my program so I'm not trying to mess it up with bad input data. I'll work on making the file IO more robust later.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement