c - Error: Expected expression before structure -
c - Error: Expected expression before structure -
i trying homecoming pointer construction maintain getting weird error bboard.c:35: error: expecte look before 'bboard' have ideas on causing this. new in c apologies of 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; (i = 0; < nrows; i++){ (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]); homecoming bboard *boardptr; } else{ printf("error"); } } /**extern void bb_display(bboard *b){ int i,j; bboard bboard = b; (i = 0; < 5; i++){ (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]); }
in c, need utilize struct bboard until you've used:
typedef struct bboard bboard; in c++, don't need create typedef.
could elaborate litle bit? dont understand.
you have:
struct bboard { int create[max_rows][max_cols]; }; bboard * bb_create(int nrows, int ncols) { … the construction definition creates type struct bboard. not create type bboard; creates struct bboard. so, when next write bboard *bb_create, there isn't type bboard, compiler complains.
if write 1 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 both define type struct bboard , alias type bboard , code compile.
in c++, defining struct or class defines type can used without struct or class prefix.
if code compiled past start of function definition, (a) aren't using standard c compiler, , (b) have problems farther downwards code at:
return bboard *boardptr; since part of:
struct bboard *boardptr = malloc(sizeof(struct bboard)); if (boardptr != null){ //printf("%d",boardptr->create[1][1]); homecoming bboard *boardptr; } else{ printf("error"); } } you don't need cast homecoming type @ all, if think do, should utilize proper cast notation (one of these two, preferably first):
homecoming boardptr; homecoming (bboard *)boardptr; the error message in else clause should more informative, should end new line, should written standard error, , should followed return 0; or equivalent.
c pointers struct
Comments
Post a Comment