bool LoadFile(CHAR_INFO *buffer[], string fileName)
{
ifstream fIn(fileName.c_str());
if(fIn.is_open())
{
WORD singleAtt;
char singleChar;
int loopCount = 0;
int stringNum = 0;
bool isAtt = false;
bool isChar = false;
string line = "";
while(fIn >> line)
{
if(isAtt == true)
{
loopCount++;
if(ConvertString<int>(stringNum, line, dec))
{
buffer[loopCount]->Attributes = stringNum;
}
else
{
return false;
}
isAtt = false;
}
else if(isChar == true)
{
loopCount++;
if(line == "SPACE")
{
singleChar = ' ';
}
else
{
singleChar = char(line.c_str());
}
buffer[loopCount]->Char.AsciiChar = singleChar;
isChar == false;
}
if(line == "Attributes:")
{
isAtt = true;
}
else if(line == "Character:")
{
isChar = true;
}
}
}
else
{
return false;
}
}
Pointer issues...
Ok, what I am trying to do is pass a pointer to an array of structs to a fucntion of mine. The issue I am running into is actually making a pointer 'point' at the array. I have looked around and can't find a lot of info. The program compiles and runs, but when I try and use the function I get an access violation. Here is the function:
I don't know if I am even declaring the funtion correctly. I want to modify the outside array, so a pointer seems the best way to handle things. The array I have is declared like so:
CHAR_INFO consoleBuffer[80*50];
A CHAR_INFO type holds the following:
union{wchar UnicodeChar, char AsciiChar}char;
WORD Attributes;
I didn't make the union, its part of the Windows.h file. From the code I have listed, how would I do the following:
1: Init a pointer to the array consoleBuffer. Explain it in words a 3 year old could understand. :p
2: Pass that pointer into my function.
3: Assign values to elements in the struct of the pointer array.
I have attempted #3 in my function as you can see but I have no idea if that is the correct way to do it. Looks like:
buffer[loopCount]->Char.AsciiChar = singleChar;
To me that seems right, but like I said I get access violations, which leads me to belive my pointer is going haywire. The sad part is I don't even know how to use my own code!! How pathetic.... Any help on this would be greatly appreciated.
Do you realize that an array *is* a pointer? Specifically, a pointer to the first element of the array? int* x and int x[] is the same thing. In other words, when passing an array to a function, there's no need to pass by reference. When you pass an array by value, you *are* passing by reference.
Quote:Original post by Wiggin
Do you realize that an array *is* a pointer? Specifically, a pointer to the first element of the array? int* x and int x[] is the same thing. In other words, when passing an array to a function, there's no need to pass by reference. When you pass an array by value, you *are* passing by reference.
OH GOSH! I had no idea! Let me change my code around and see I can get it to work.
If you do pass by reference, you can muck things up by passing a pointer to the pointer to the first element, and then trying to access an element before dereferencing. In other words, an access violation.
Quote:Original post by Wiggin
Do you realize that an array *is* a pointer? Specifically, a pointer to the first element of the array? int* x and int x[] is the same thing. In other words, when passing an array to a function, there's no need to pass by reference. When you pass an array by value, you *are* passing by reference.
That's not quite true. An array is more equivalent to a constant pointer (int a[] -> int * const p), and it also has a different size. sizeof( a ) is the size in bytes of the entire array, while sizeof( p ) is the size of the pointer. When passing an array to a function, it can decay to a pointer:
void ByPointer( CHAR_INFO buffer[] ); (implicit)
which is equivalent to void ByPointer( CHAR_INFO * const buffer );
Or you can do void ByPointer( CHAR_INFO *buffer );, if you want to "iterate" your array with that identifier.
You can also pass an array to a function by reference, thereby avoiding the decay (and preserving size information):
template< size_t size >
void ByReference( CHAR_INFO ( &buffer )[size] );
That said, you can avoid the syntax issues by using an array wrapper such as boost::array:
template< size_t size >
void ByWrapper( boost::array< CHAR_INFO, size > &buffer );
jfl.
[Edited by - jflanglois on July 28, 2006 9:42:27 PM]
Well I don't get an access violation anymore! Thanks for that info, I had NO IDEA. Sadly thouhg my program just exits when I try to use that function now. Heheh. Im still at the stage of programming where you type a bunch of stuff up, cross your fingers and hit compile. I may not be reading the file back in correctly. The file looks like so:
Attributes: 16 Character: D
Attributes: 16 Character: SPACE
Attributes: 16 Character: SPACE
Attributes: 16 Character: SPACE
Attributes: 16 Character: Û
Attributes: 16 Character: Û
As you can see I am trying to read in white space seperated words. When the word == "Attributes:" I assing 16 (or whatever) to buffer.Attributes. When the word == "Character:" I assing the 'D' or ' ' or whatever else might be listed to buffer.Char.AsciiChar. I am looking over my logic and stuff now, is there something wrong with it that I am missing?
Attributes: 16 Character: D
Attributes: 16 Character: SPACE
Attributes: 16 Character: SPACE
Attributes: 16 Character: SPACE
Attributes: 16 Character: Û
Attributes: 16 Character: Û
As you can see I am trying to read in white space seperated words. When the word == "Attributes:" I assing 16 (or whatever) to buffer.Attributes. When the word == "Character:" I assing the 'D' or ' ' or whatever else might be listed to buffer.Char.AsciiChar. I am looking over my logic and stuff now, is there something wrong with it that I am missing?
Quote:Original post by -JetSirus-
Well I don't get an access violation anymore! Thanks for that info, I had NO IDEA. Sadly thouhg my program just exits when I try to use that function now. Heheh. Im still that the stage of programming where you type a bunch of stuff up, cross your fingers and hit compile. I may not be reading the file back in correctly. The file looks like so:
Attributes: 16 Character: D
Attributes: 16 Character: SPACE
Attributes: 16 Character: SPACE
Attributes: 16 Character: SPACE
Attributes: 16 Character: Û
Attributes: 16 Character: Û
As you can see I am trying to read in white space seperated words. When the word == "Attributes:" I assing 16 (or whatever) to buffer.Attributes. When the word == "Character:" I assing the 'D' or ' ' or whatever else might be listed to buffer.Char.AsciiChar. I am looking over my logic and stuff now, is there something wrong with it that I am missing?
I suggest you simplify your file format to something implicit. The above would be like (note that where it's blank is actually a space):
16D16 16 16 16Û16Û
And then you can use formatted input for the numbers, and ifstream::read() for the character.
[edit] Your function would now look like:
bool LoadFile( CHAR_INFO buffer[], std::string const &fileName ) { std::ifstream fin( fileName.c_str() ); if( !fin.is_open() ) return false; WORD attrib = 0; char ch = 0; for( int unsigned i = 0; fin >> attrib; ++i ) { fin.ignore(); fin.read( &ch, 1 ); buffer.Attributes = attrib; buffer.Char.AsciiChar = ch; } return true;}
[Edited by - jflanglois on July 28, 2006 9:53:55 PM]
Quote:Original post by Wiggin
Do you realize that an array *is* a pointer? Specifically, a pointer to the first element of the array? int* x and int x[] is the same thing. In other words, when passing an array to a function, there's no need to pass by reference. When you pass an array by value, you *are* passing by reference.
If an array was a pointer, it would be pointing to an address of a variable of type; int. An array is the object - or variable - you will be manipulating, while, a pointer is pointing at an object you will be manipulating (once'd assigned to).
- An array name yields the address of itself.
- An pointer name yields the address of another variable (once initialized that is)
- xeddiex
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement