1

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;
3
  • You never assign a location to these pointers. Commented Apr 10, 2020 at 16:48
  • Can you please elaborate, im new to this more "complex" C projects. Commented Apr 10, 2020 at 16:52
  • Good attempt at information hiding. Commented Apr 10, 2020 at 17:02

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.