Reputation: 245
I am trying to return a pointer to a structure but I keep getting this weird error bboard.c:35: error: Expecte expression before 'BBoard'
Does anyone have any ideas on to what could be causing this. I fairly new in c so my apologies of this is a trivial question.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "bboard.h"
struct BBoard {
int create[MAX_ROWS][MAX_COLS];
};
BBoard * bb_create(int nrows, int ncols){
int i,j;
srand(time(NULL));
struct BBoard Bboard;
for (i = 0; i < nrows; i++){
for (j = 0; j < ncols; j++){
int r = rand() % 4;
//printf("%d",r);
if( r == 0){
Bboard.create[i][j] = 1;}
if(r == 1){
Bboard.create[i][j] = 2;}
if(r == 2){
Bboard.create[i][j] = 3;}
if(r == 3){
Bboard.create[i][j] = 4;}
printf(" %d ",Bboard.create[i][j]);
}
printf("\n");
}
struct BBoard *boardPtr = malloc(sizeof(struct BBoard));
if (boardPtr != NULL){
//printf("%d",boardPtr->create[1][1]);
return BBoard *boardPtr;
}
else{
printf("Error");
}
}
/**extern void bb_display(BBoard *b){
int i,j;
BBoard Bboard = b;
for (i = 0; i < 5; i++){
for (j = 0; j < 5; j++){
printf("%d",Bboard.create[i][j]);
}
}
}**/
int main(){
BBoard *bptr;
bptr = bb_create(5,5);
//printf("%d",bptr->create[0][1]);
}
Upvotes: 0
Views: 4667
Reputation: 753475
In C, you need to use struct BBoard
until you've used:
typedef struct BBoard BBoard;
In C++, you don't need to create the typedef.
Could you elaborate a litle bit? I dont understand.
You have:
struct BBoard
{
int create[MAX_ROWS][MAX_COLS];
};
BBoard * bb_create(int nrows, int ncols)
{
…
The structure definition creates a type struct BBoard
. It does not create a type BBoard
; it only creates struct BBoard
. So, when you next write BBoard *bb_create
, there isn't a type BBoard
, so the compiler complains.
If you write one of these sequences — either:
typedef struct BBoard
{
int create[MAX_ROWS][MAX_COLS];
} BBoard;
or:
typedef struct BBoard BBoard;
struct BBoard
{
int create[MAX_ROWS][MAX_COLS];
};
or:
struct BBoard
{
int create[MAX_ROWS][MAX_COLS];
};
typedef struct BBoard BBoard;
then you both define a type struct BBoard
and an alias for the type BBoard
and then your code will compile.
In C++, simply defining a struct
or class
defines a type which can be used without the struct
or class
prefix.
If your code compiled past the start of the function definition, then (a) you aren't using a standard C compiler, and (b) you have problems further down your code at:
return BBoard *boardPtr;
Since this is part of:
struct BBoard *boardPtr = malloc(sizeof(struct BBoard));
if (boardPtr != NULL){
//printf("%d",boardPtr->create[1][1]);
return BBoard *boardPtr;
}
else{
printf("Error");
}
}
You really don't need to cast the return type at all, but if you think you do, you should use proper cast notation (one of these two, preferably the first):
return boardPtr;
return (BBoard *)boardPtr;
The error message in the else
clause should be more informative, should end with a new line, should be written to standard error, and should be followed by a return 0;
or equivalent.
Upvotes: 3