Help with pointers please!!!!

Started by
9 comments, last by SPatuel 22 years, 2 months ago
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
Syco - Spatuel
Advertisement
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!
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.left=Valor;<br> Cuadros.right=Valor+31;<br> Cuadros.bottom=Valor2+31;<br><br> Valor=Valor+32;<br> if (i==2) <br> {<br> Valor2=32;<br> Valor=0;<br> }<br> }<br> <br> //I read a TXT with numbers (0 to 5)<br> if ((ioArchivo=fopen("Mapa.txt","r+t"))!=NULL)<br> Contador=fread(Mapa , sizeof(char), 44, ioArchivo);<br> else<br> {<br> Error("No se encuentra el archivo Mapa.map");<br> fclose(ioArchivo);<br> return FALSE;<br> }<br> fclose(ioArchivo);<br><br> } <br> <br> //I show the background<br> Contador=0;<br> for (int x=0;x<WIDTH;x=x+32)<br> <br> for (int y=0;y<HEIGHT;y=y+32)<br> {<br> if (!ShowSprite(Textura[0],x,y,&Cuadros[Mapa[Contador]])) return FALSE;<br> Contador++;<br> }<br> <br> return TRUE;<br><br><br>}<br><br><br>BOOL Juego::ShowSprite(LPDIRECT3DTEXTURE8 Textura,float x,float y,RECT* Cuadro)<br>{<br><br> D3DXVECTOR2 Posicion(x,y);<br><br> <br> //**I present the box<br> if (FAILED(pSprite->Draw(<br> Textura, //LPDIRECT3DTEXTURE8 pSrcTexture,<br> ((Cuadro)?Cuadro:NULL), //CONST RECT* pSrcRect,<br> NULL, //CONST D3DXVECTOR2* pScaling,<br> NULL, //CONST D3DXVECTOR2* pRotationCenter,<br> 0, //FLOAT Rotation,<br> &Posicion, //CONST D3DXVECTOR2* pTranslation,<br> D3DCOLOR_RGBA(255,255,255,255) //D3DCOLOR Color<br> )))<br> {<br> Error("No se pudo mostrar el menu del juego por falla de Sprite");<br> return FALSE;<br> }<br><br> return TRUE;<br><br>}<br><br>I really appreciate your help<br> </i> <br><br>Syco - Spatuel<br>
Syco - Spatuel
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!
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
Syco - Spatuel
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!
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
Syco - Spatuel
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!
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
Syco - Spatuel
No problem. Glad I could help.

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!

This topic is closed to new replies.

Advertisement