marxum
marxum

Reputation: 13

Declaring variable in header and defining in .c

My objetive here is to use the variables defined in templates.c in another .c file.

I am trying to define variables of type SHIP(struct that I created) declared in the templates.h file, but i get this error (i get a similar error for the rest of the variables:

templates.c:5:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘->’ token
 SHIP* Pickaxe->bitmap={  {'0','1','1','1','0'},
              ^~
templates.c:10:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’ before ‘->’ token
 SHIP* Pickaxe->size=9;
              ^~

here's templates.h

typedef struct{
    char bitmap[5][5];
    int size;
}SHIP;

extern SHIP *Pickaxe;
extern SHIP *Carrier;
extern SHIP *Battleship;
extern SHIP *Submarine;
extern SHIP *Cruiser;
extern SHIP *Destroyer;

and the templates.c file where im defining the variables

#include<stdio.h>
#include"templates.h"
#include<stdlib.h>

Pickaxe->bitmap={  {'0','1','1','1','0'},
                   {'1','0','1','0','1'},
                   {'0','0','1','0','0'},
                   {'0','0','1','0','0'},
                   {'0','0','1','0','0'}};
Pickaxe->size=9;


Carrier->bitmap={{'0','0','1','0','0'},
                 {'0','0','1','0','0'},
                 {'0','0','1','0','0'},
                 {'0','0','1','0','0'},
                 {'0','0','1','0','0'}};
Carrier->size=5;

Battleship->bitmap={{'0','0','1','0','0'},
                    {'0','0','1','0','0'},
                    {'0','0','1','0','0'},
                    {'0','0','1','0','0'},
                    {'0','0','0','0','0'}};
Battleship->size=4;

Submarine->bitmap={{'0','0','0','0','0'},
                   {'0','0','1','0','0'},
                   {'0','0','1','0','0'},
                   {'0','0','1','0','0'},
                   {'0','0','0','0','0'}};
Submarine->size=3;

Cruiser->bitmap={{'0','0','0','0','0'},
                 {'0','0','1','0','0'},
                 {'0','0','1','0','0'},
                 {'0','0','1','0','0'},
                 {'0','0','0','0','0'}};
Cruiser->size=3;

Destroyer->bitmap={{'0','0','0','0','0'},
                   {'0','0','1','0','0'},
                   {'0','0','1','0','0'},
                   {'0','0','0','0','0'},
                   {'0','0','0','0','0'}};
Destroyer->size=2;

Upvotes: 1

Views: 50

Answers (1)

chux
chux

Reputation: 154601

In the .c file, pointer Pickaxe and others need to be defined and given a value. Example:

// Form the data.  Make static not to clutter global name space.
static SHIP Pickaxe_data = {.size = 9, //
    .bitmap = { //
        {'0', '1', '1', '1', '0'}, //
        {'1', '0', '1', '0', '1'}, // 
        {'0', '0', '1', '0', '0'}, //
        {'0', '0', '1', '0', '0'}, //
        {'0', '0', '1', '0', '0'}}};

SHIP *Pickaxe = &Pickaxe_data;

Advanced:

If this data should not get changed, make const. Example:

// .h    
extern const SHIP * const Pickaxe;
//                  ^^^^^ ----- Pointer should not get changed
//     ^^^^^ ------------------ Data referenced should not get changed     

// .c
static const SHIP Pickaxe_data = {.size = 9, //
    .bitmap = { //
        {'0', '1', '1', '1', '0'}, //
        ...
        {'0', '0', '1', '0', '0'}}};

const SHIP * const Pickaxe = &Pickaxe_data;

Upvotes: 1

Related Questions