Codeblock写C语言代码报错 expected ';', ',' or ')' before '&' token

Codeblocks中写c语言代码,在编译时候报错:

error: expected ':', ',', ';', '}' or '__attribute__' before '=' token

错误原因:

在C语言中是不存在引用,C语言中&表示的不是引用,仅仅是取地址符。

因此,应该使用指针代替引用, 在主函数中传入地址注意C语言中‘.’和‘->’的区别。

报错代码如下

代码用途:C语言实现栈的初始化和入栈操作

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

#define STACK_INIT_SIZE 100
#define STACKINCREACE 10
typedef int Elemtype;
typedef int Status;
typedef struct{
    Elemtype *base;
    Elemtype *top;
    int stacksize;
}SqStack;

Status InitStack(SqStack &S);
Status Push(SqStack &S,Elemtype e);
Status StackTraverse(SqStack S);

Status InitStack(SqStack &S){
    S.base = (Elemtype *)malloc(STACK_INIT_SIZE*sizeof(Elemtype));
    if(!S.base){
        return false;
    }
    S.stacksize=STACK_INIT_SIZE;
    S.top=S.base;
    return true;
}

//---------------------入栈函数---------------------

Status Push(SqStack &S,Elemtype e){
//判断是否溢出
    if(S.top-S.base>=S.stacksize){
        S.base=(Elemtype *)realloc(S.base,(S.stacksize+STACKINCREACE)*sizeof(Elemtype));
        if(!S.base){
            return false;
        }
        S.top=S.base+S.stacksize;//注意因为这里的栈底指针的改变,导致栈顶指针随之改变
        S.stacksize+=STACKINCREACE;
    }
//压栈部分
    *S.top=e;
    S.top++;//栈顶指针加一
    return true;
}

Status StackTraverse(SqStack S){//从栈底到栈顶的方向
    if(S.top==S.base){
        return false;
    }
    while(S.base <S.top ){
        printf("%d\t",*(S.base++));
    }
    printf("\n");
    return true;
}
int main(){
    SqStack stack;
    InitStack(stack);

    Push(stack,1);
    Push(stack,2);
    Push(stack,3);
    StackTraverse(stack);
}

修改后的正确代码:

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

#define STACK_INIT_SIZE 100
#define STACKINCREACE 10
typedef int Elemtype;
typedef int Status;
typedef struct{
    Elemtype *base;
    Elemtype *top;
    int stacksize;
}SqStack;

Status InitStack(SqStack *S);
Status Push(SqStack *S,Elemtype e);
Status StackTraverse(SqStack S);

Status InitStack(SqStack *S){
    S->base = (Elemtype *)malloc(STACK_INIT_SIZE*sizeof(Elemtype));
    if(!S->base){
        return -1;
    }
    S->stacksize=STACK_INIT_SIZE;
    S->top=S->base;
    return 0;
}

//---------------------入栈函数---------------------

Status Push(SqStack *S,Elemtype e){
//判断是否溢出
    if(S->top - S->base >= S->stacksize){
        S->base=(Elemtype *)realloc(S->base,(S->stacksize+STACKINCREACE)*sizeof(Elemtype));
        if(!S->base){
            return -1;
        }
        S->top=S->base+S->stacksize;//注意因为这里的栈底指针的改变,导致栈顶指针随之改变
        S->stacksize+=STACKINCREACE;
    }
//压栈部分
    *(S->top) = e;
    S->top++;//栈顶指针加一
    return 0;
}

Status StackTraverse(SqStack S){//从栈底到栈顶的方向
    if(S.top==S.base){
        return -1;
    }
    while(S.base <S.top ){
        S.top--;
        printf("%d\t",*(S.top));
    }
    printf("\n");
    return 0;
}
int main(){
    SqStack stack;
    InitStack(&stack);

    Push(&stack,1);
    Push(&stack,2);
    Push(&stack,3);
    StackTraverse(stack);
}

运行结果:

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用 Code::Blocks IDE 编C语言围棋代码,包括吃子和禁手规则。 ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define BOARD_SIZE 19 #define EMPTY 0 #define BLACK 1 #define WHITE 2 int board[BOARD_SIZE][BOARD_SIZE]; int current_player = BLACK; void print_board(){ printf(" "); for(int i=0;i<BOARD_SIZE;++i){ printf("%c ", 'A'+i); } printf("\n"); for(int i=0;i<BOARD_SIZE;++i){ printf("%2d", i+1); for(int j=0;j<BOARD_SIZE;++j){ if(board[i][j] == EMPTY){ printf(" +"); } else if(board[i][j] == BLACK){ printf(" X"); } else{ printf(" O"); } } printf("\n"); } } int is_within_board(int x, int y){ return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE; } int is_surrounded(int x, int y){ if(!is_within_board(x, y) || board[x][y] != EMPTY){ return 0; } int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; for(int i=0;i<4;++i){ int nx = x + dx[i], ny = y + dy[i]; if(is_within_board(nx, ny) && board[nx][ny] == EMPTY){ return 0; } } return 1; } int is_legal_move(int x, int y){ if(!is_within_board(x, y) || board[x][y] != EMPTY){ return 0; } int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; for(int i=0;i<4;++i){ int nx = x + dx[i], ny = y + dy[i]; if(is_within_board(nx, ny) && board[nx][ny] == current_player){ if(is_surrounded(nx, ny)){ return 1; } } } return 0; } int is_ko(int x, int y){ int tmp_board[BOARD_SIZE][BOARD_SIZE]; memcpy(tmp_board, board, sizeof(board)); board[x][y] = current_player; int captured = 0; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; for(int i=0;i<4;++i){ int nx = x + dx[i], ny = y + dy[i]; if(is_within_board(nx, ny) && board[nx][ny] == 3 - current_player){ if(is_surrounded(nx, ny)){ captured += 1; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; for(int j=0;j<4;++j){ int nnx = nx + dx[j], nny = ny + dy[j]; if(is_within_board(nnx, nny) && board[nnx][nny] == current_player){ if(is_surrounded(nnx, nny)){ board[nx][ny] = EMPTY; } } } } } } int res = 0; if(captured == 1){ int cnt = 0; for(int i=0;i<BOARD_SIZE;++i){ for(int j=0;j<BOARD_SIZE;++j){ if(board[i][j] != tmp_board[i][j]){ cnt += 1; } } } res = cnt == 1; } memcpy(board, tmp_board, sizeof(board)); return res; } void do_move(int x, int y){ if(is_legal_move(x, y)){ if(is_ko(x, y)){ printf("Illegal move: ko rule\n"); return; } board[x][y] = current_player; int captured = 0; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; for(int i=0;i<4;++i){ int nx = x + dx[i], ny = y + dy[i]; if(is_within_board(nx, ny) && board[nx][ny] == 3 - current_player){ if(is_surrounded(nx, ny)){ captured += 1; int dx[] = {-1, 1, 0, 0}; int dy[] = {0, 0, -1, 1}; for(int j=0;j<4;++j){ int nnx = nx + dx[j], nny = ny + dy[j]; if(is_within_board(nnx, nny) && board[nnx][nny] == current_player){ if(is_surrounded(nnx, nny)){ board[nx][ny] = EMPTY; } } } } } } if(captured == 0){ current_player = 3 - current_player; } } else{ printf("Illegal move\n"); } } int main(){ memset(board, EMPTY, sizeof(board)); while(1){ print_board(); printf("Current player: %s\n", current_player == BLACK ? "Black(X)" : "White(O)"); printf("Enter move (e.g. A1): "); char move[10]; scanf("%s", move); if(strcmp(move, "q") == 0 || strcmp(move, "quit") == 0){ break; } if(strlen(move) != 2){ printf("Invalid input\n"); continue; } int x = move[1] - '1', y = move[0] - 'A'; do_move(x, y); } return 0; } ``` 在这个代码中,我们使用了一个二维数组来表示围棋棋盘,其中 `EMPTY`, `BLACK` 和 `WHITE` 分别代表空位、黑子和白子。我们还定义了一个 `current_player` 变量来表示当前玩家。在 `print_board()` 函数中,我们打印出当前棋盘的状态。 接下来我们定义了一些辅助函数,用于判断一个位置是否在棋盘内、是否被包围、是否可以合法落子等。在 `is_legal_move()` 函数中,我们首先判断目标位置是否在棋盘内且为空位,然后检查该位置周围是否存在己方棋子被包围的情况,如果存在,则说明该位置可以合法落子。 在 `is_ko()` 函数中,我们先判断落子后是否会有对手的棋子被提掉,如果存在,则进一步判断这个提掉的棋子是否唯一。如果唯一,则说明这是一个禁着点,不能落子,否则可以落子。 在 `do_move()` 函数中,我们首先检查目标位置是否可以合法落子,如果合法,则更新棋盘状态,并检查是否有对手的棋子被提掉。如果没有,则交换当前玩家。 最后,在主函数中,我们循环读入玩家的落子,并调用 `do_move()` 函数来更新棋盘状态。如果玩家输入 `q` 或 `quit`,则退出游戏。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值