Archived

This topic is now archived and is closed to further replies.

SPatuel

Help with pointers please!!!!

Recommended Posts

Hi, i am trying to send by reference a RECT struct to a function, but when i pass it the address is wrong and my function use a struct with trash. Why is this happend? RECT Box[4]; Box.top=0; Box.bottom=31; Box.left=0; Box.right=31; myfunction(&Box[0]); //When i check the address is 0x0012fda4 ----------- void myfunction(RECT* RefBox) // When i debug and check the address is another !!! 0x0012fd10 and my box is top=0, bottom=1223333, left=144,right=222222 (aparently trash) Please can anybody help me!!!!! Syco - Spatuel

Share this post


Link to post
Share on other sites
quote:
Original post by SPatuel
RECT Box[4];

Box.top=0;
Box.bottom=31;
Box.left=0;
Box.right=31;

Did you accidentally omit the indices here ([0])?

Anyway, nothing seems to be wrong with your code, from the abstract snippet you posted. What does the body of myfunction() look like? I suspect the error may lie in how you attempt to access the data members (are you dereferencing properly?)

I wanna work for Microsoft!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
I am putting the original code....

BOOL Juego::MuestraFondo()
{
//Boxes
RECT Cuadros[6];
char Mapa[44];
BYTE Contador=0;

//Is the Box empty?
if (Cuadros[0].bottom!=31)
{
BYTE Valor=0,Valor2=0;

// I fill the boxes
for (int i=0;i<6;i++)
{
Cuadros.top=Valor2;
Cuadros[i].left=Valor;
Cuadros[i].right=Valor+31;
Cuadros[i].bottom=Valor2+31;

Valor=Valor+32;
if (i==2)
{
Valor2=32;
Valor=0;
}
}

//I read a TXT with numbers (0 to 5)
if ((ioArchivo=fopen("Mapa.txt","r+t"))!=NULL)
Contador=fread(Mapa , sizeof(char), 44, ioArchivo);
else
{
Error("No se encuentra el archivo Mapa.map");
fclose(ioArchivo);
return FALSE;
}
fclose(ioArchivo);

}

//I show the background
Contador=0;
for (int x=0;x
for (int y=0;y {
if (!ShowSprite(Textura[0],x,y,&Cuadros[Mapa[Contador]])) return FALSE;
Contador++;
}

return TRUE;


}


BOOL Juego::ShowSprite(LPDIRECT3DTEXTURE8 Textura,float x,float y,RECT* Cuadro)
{

D3DXVECTOR2 Posicion(x,y);


//**I present the box
if (FAILED(pSprite->Draw(
Textura, //LPDIRECT3DTEXTURE8 pSrcTexture,
((Cuadro)?Cuadro:NULL), //CONST RECT* pSrcRect,
NULL, //CONST D3DXVECTOR2* pScaling,
NULL, //CONST D3DXVECTOR2* pRotationCenter,
0, //FLOAT Rotation,
&Posicion, //CONST D3DXVECTOR2* pTranslation,
D3DCOLOR_RGBA(255,255,255,255) //D3DCOLOR Color
)))
{
Error("No se pudo mostrar el menu del juego por falla de Sprite");
return FALSE;
}

return TRUE;

}

I really appreciate your help


Syco - Spatuel

Share this post


Link to post
Share on other sites
You define Cuadros as
quote:
Original post by SPatuel
RECT Cuadros[6];

But then access it as
quote:
Original post by SPatuel
if (!ShowSprite(Textura[0],x,y,&Cuadros[Mapa[Contador]])) return FALSE;

Are you sure that Mapa[Contador] is giving you a valid index? I suggest you use the MSVC debugger (press F9 to place a breakpoint on a line, F5 to start debugging, F10 to step over line-by-line after you''ve started debugging, F11 to step into a function call and Shift+F11 to step out). Set a breakpoint on the ShowSprite function call and watch the value of Mapa[Contador] in the Watch window. If it ever exceeds 5 - there''s your error!

I wanna work for Microsoft!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
Ohhhhhhhhhh!!!! i seeee

when i watch ie Mapas[0] its shows "48 ''0''" i think this is the ascii code, not the value!.

Thanks a lot for your help!!!!!!!!!

Syco - Spatuel

Share this post


Link to post
Share on other sites
The error lies where you read Mapa in from the text file. You might want to look into fscanf to read formatted data (so it converts the characters in the file into integers).

I wanna work for Microsoft!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
ok, i see i modified that, when i put my cursor over Mapa its shows "0000000000000000000000000000000000000" thats ok, but when i quick watch Mapa[0] still shows "48 ''0''". Is there a function in C++ that converts ASCII to STRING? (like VB ''s CHR() function)?

Thanks a lot

Syco - Spatuel

Share this post


Link to post
Share on other sites
What format specifier did you use in fscanf? For a decimal integer it should be %d. That said, ASCII is "string" (C does not actually have a native string type, remember?). You can convert ASCII to integer (atoi), long integer (atolatof) and double (atod), as well as the reverse integer to ASCII operations (itoa, ltoa) and their unsigned variants.

If you''re using C++, though, I''d suggest you ditch fopen and siblings and use fstreams instead:

#include <fstream>
#include <iostream>
#include <string>
//
int main(void)
{
// open file
std::ifstream fin; // Input File Stream
fin.open("filename.ext");
if(fin.fail())
{
std::cerr << "Failed to open filename.ext" << std::endl;
return -1;
}
//
// read file data
int n;
std::string str, line;
while(fin.good())
{
// you can read in like cin:
fin >> n >> str; // reads an integer and a string, seperated by whitespace
// you can use getline to read in an entire line
std::getline(fin, line);
}
//
// close file
fin.close();
return 0;
}

Because C++ is typesafe and strict about implicit conversions, it''ll help you catch little "type errors". Because ifstream is a class with operator<< and operator>> overloaded for all basic types, it automatically knows how to read data into a given variable. You can also overload those operators for any of your own classes.

I wanna work for Microsoft!
[ GDNet Start Here | GDNet Search Tool | GDNet FAQ | MS RTFM [MSDN] | SGI STL Docs | Google! ]
Thanks to Kylotan for the idea!

Share this post


Link to post
Share on other sites
wow, finally i did it!!!

i used yout fstream functions but replace the string variable with a byte array and works perfect!

Thanks a lot for your time!


Syco - Spatuel

Share this post


Link to post
Share on other sites