gpt4 book ai didi

c - 错误: expected identifier before '(' token appearing 13 times

转载 作者:行者123 更新时间:2023-12-02 10:52:51 25 4
gpt4 key购买 nike

我知道这不完全是一个“问题”,但我已尽力做到尽可能简洁。

我不断收到此错误:

错误:'(' token 之前的预期标识符为13次。

我检查了我是否

1)有多余/缺少任何括号

2)添加/缺少任何分号

3)缺少任何头文件

In function 'goToFloor':

Line 86|error: expected identifier before '(' token
Line 88|error: expected identifier before '(' token
Line 90|error: expected identifier before '(' token
Line 94|error: expected identifier before '(' token
Line 99|error: expected identifier before '(' token
Line 101|error: expected identifier before '(' token
Line 102|error: expected identifier before '(' token
Line 106|error: expected identifier before '(' token
Line 107|error: expected identifier before '(' token
Line 112|error: expected identifier before '(' token

In function 'printElevators':

Line 122|error: expected identifier before '(' token
Line 123|error: expected identifier before '(' token
Line 124|error: expected identifier before '(' token

||=== Build failed: 13 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

我已经包含了整个代码,而不仅仅是错误发生的位置,因为我感觉到错误不在其中
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define CAPACITY 15
#define MAX_LEN 21


typedef struct elevator_t
{
int floor;
int passenger;
int usage;

} elevator_t;

//main function
void setupElevators(elevator_t [], int);
void readSequences(char [][MAX_LEN], int);
void runElevators(elevator_t * elevators, char (* sequences)[MAX_LEN], int size);
void printElevators(elevator_t [], int size);
int mostUsedElevator(elevator_t [], int);

//sub functions
void goToFloor(elevator_t *elevator, int floor);//local to runElevator


int main(void)
{
int size;
elevator_t * elevators;
char (* sequences)[MAX_LEN];

printf("Enter number of elevators( 1 - 20 )PS I'm not validating: ");
scanf(" %d", &size);

setupElevators(elevators, size);
readSequences(sequences, size);

runElevators(elevators, sequences, size);

printElevators(elevators, size);
//printf("Most used elevator: %d\n", mostUsedElevator(elevators, size));

return 0;
}


void dynamAlloc(elevator_t ** elevators,char (** sequences)[MAX_LEN],int size)
{
*elevators=calloc( size, sizeof(**elevators) ); //wanna intialize everyting to 0
*sequences=calloc( size, sizeof(**sequences) );
}


// Read in the sequences of floors the elevators go to.
void readSequences(char (*sequences)[MAX_LEN], int size)
{
int i;

for (i = 0; i<size; i++)
{
printf("Enter sequence for elevator %d: ", i+1);
scanf(" %s", sequences[i]);
}
}

void runElevators(elevator_t * elevators, char (* sequences)[MAX_LEN], int size)
{
int floor_number=0,elevator_number;

for(elevator_number=0; elevator_number<=size-1; elevator_number++)
{
while(sequences[elevator_number][floor_number]!='\0')
{
goToFloor(&elevators[elevator_number],floor_number);
floor_number++;
}
}
}

void goToFloor(elevator_t *elevator, int floor)
{
if( floor> (*elevator).(floor) )//going up, passengers are entering
{
if( (*elevator).(passenger) + floor >CAPACITY ) // cant take more than 15
{
(*elevator).(passenger)=CAPACITY;
}
else //<=15
{
(*elevator).(passenger)+= floor;
}
}
else//going down, passengers are exiting
{
if( floor<=(*elevator).(passenger)) //whichever amount is lower will be the amount
{
(*elevator).(passenger)-=floor;
(*elevator).(usage)+=floor;
}
else
{
(*elevator).(usage)+=(*elevator).(passenger);
(*elevator).(passenger)-=(*elevator).(passenger);

}
}

(*elevator).(floor)=floor;//update current floor lift is at
}

void printElevators(elevator_t elevators[], int size)
{
int elevator_number;

for(elevator_number=0;elevator_number<=size-1;elevator_number++)
{
printf("Elevator %d:\n",elevator_number+1);
printf("Floor: \n",( elevators[elevator_number] ).(floor));
printf("Number of passengers: \n",( elevators[elevator_number] ).(passenger) );
printf("Usage: \n", ( elevators[elevator_number] ).(usage) );
}
}


int mostUsedElevator(elevator_t elevators[], int size)
{
// Incomplete
return 1;
}

最佳答案

这些错误通常是围绕这样的构造:

(*elevator).(passenger)=CAPACITY;

prog.c:88:25: error: expected identifier before ‘(’ token
(*elevator).(passenger)=CAPACITY;
^---------Error Here

这是使用结构的一种非常奇怪,晦涩且不正确的方法。
. 后面必须是是结构的字段。尝试使用 ( parens )意味着它是要求值的表达式。但是此处的表达式无效,它必须是字段名。

应该是:
elevator->passenger = CAPACITY;

关于c - 错误: expected identifier before '(' token appearing 13 times,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62226656/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com