Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newb question about table....

Status
Not open for further replies.

lobodo44

Technical User
Sep 8, 2003
27
CA
Hi there,
ok i'm pretty new to programming so this is maybe or surely not specific to borland builer but anyway......OK i want to make a checkers game so i declare a kind ...


typedef enum {
Piece1,Piece2, Piece3,Piece4,Piece5,Piece6,Piece7,Piece8,Piece9
,Piece10,Piece11,Piece12,aucun
} pieces_name;

typedef enum {
red,black,none
} pieces_color;

struct piece_t {

pieces_name kind;
pieces_color color;

};

Its for the piece. Making a structure for a piece with his kind and his color. I have made a table representing the board.



So I tried to put piece in it but it gave me an error of 'EAccessViolation'. Here is what I started.

for (int j=0;j<=3;j++){

for (int i=0;i<=7;i+2) {
tab[j]->color = red ;
}
}
}

This 'for' is not finish... Please help me ...Here is what could be inflencing I dont know...(header of declaration)


//////////////////////////Main.h////////////////////////

//---------------------------------------------------------------------------
#ifndef MainH
#define MainH
//---------------------------------------------------------------------------
#include <sysutils.hpp>
#include <windows.hpp>
#include <messages.hpp>
#include <sysutils.hpp>
#include <classes.hpp>
#include <graphics.hpp>
#include <controls.hpp>
#include <forms.hpp>
#include <dialogs.hpp>
#include <stdctrls.hpp>
#include <buttons.hpp>
#include <extctrls.hpp>
#include <menus.hpp>
#include <Buttons.hpp>
#include <Classes.hpp>
#include <Controls.hpp>
#include <Dialogs.hpp>
#include <ExtCtrls.hpp>
#include <Menus.hpp>
#include <Graphics.hpp>
#include <StdCtrls.hpp>
#include <jpeg.hpp>

typedef enum {
Piece1,Piece2, Piece3,Piece4,Piece5,Piece6,Piece7,Piece8,Piece9
,Piece10,Piece11,Piece12,aucun
} pieces_name;

typedef enum {
red,black,none
} pieces_color;

struct piece_t {

pieces_name kind;
pieces_color color;

};
piece_t *tab[8][8] ={
{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL},
{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL},
{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL},
{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL},
{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL},
{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL},
{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL},
{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL},
};
//---------------------------------------------------------------------------
class TMainForm : public TForm
{
__published:
TMainMenu *MainMenu;
TMenuItem *FileNewItem;
TMenuItem *FileOpenItem;
TMenuItem *FileSaveItem;
TMenuItem *FileSaveAsItem;
TMenuItem *FileExitItem;
TOpenDialog *OpenDialog;
TSaveDialog *SaveDialog;
TPanel *SpeedBar;
TSpeedButton *SpeedButton1; // &New
TSpeedButton *SpeedButton2; // &Open...
TSpeedButton *SpeedButton3; // &Save
TSpeedButton *SpeedButton4; // Save &As...
TSpeedButton *SpeedButton5;
TImage *RPiece1;
TEdit *testing;
TImage *imgChessboard;
TButton *Button1; // E&xit
void __fastcall FileNew(TObject *Sender);
void __fastcall FileOpen(TObject *Sender);
void __fastcall FileSave(TObject *Sender);
void __fastcall FileSaveAs(TObject *Sender);
void __fastcall FileExit(TObject *Sender);
void __fastcall test(TObject *Sender);
bool __fastcall MoveAllowed (int xstart,int ystart, int xend,int yend);
private: // private user declarations
public: // public user declarations
virtual __fastcall TMainForm(TComponent* Owner);
piece_t *tab[8][8];
};


//---------------------------------------------------------------------------
extern TMainForm *MainForm;
//---------------------------------------------------------------------------
#endif
 
I think the problem is this bit...

piece_t *tab[8][8] ={
{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL},
{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL},
{NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL},
....

You are actually declaring pointers to and not actual instances of 'tab'.

I suspect if you looked tat the value of tab in the for .. next statement at the point it fails you'll find that the value of tab is NULL.. hence the Access Violation...

You need to set up the tab array first , using code like:

tab[j] = new piece_t

before you can assign any values to the variables...

Alternatively....

change the code so that the declaration of

piece_t *tab[8][8] actually reads :

piece_t tab[8][8] then initialize each element so in full it would be :

piece_t tab[8][8] = {
{ Piece1 , red }
{ Piece2 , black }
etc.

Hope this helps .. or at least makes sense...

Regards





 
Thanks man :) You really helped me ! My program is not near from end but I can continue. Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top