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

I know this is not exactly a "question" but I have tried my best to present neatly as possible.
I keep getting this error:
Error: expected identifier before '(' token for 13 times.
I've checked if I
1) have extra/missing any parenthesis
2) added/missed any semicolons
3) Missed any header files
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)) ===|
I've included the whole code and not just where the errors happened as I have a feeling the errors are not in them
#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;
}

These errors are frequently around constructs like this:
(*elevator).(passenger)=CAPACITY;
prog.c:88:25: error: expected identifier before ‘(’ token
(*elevator).(passenger)=CAPACITY;
^---------Error Here
That is a very strange and obtuse, and incorrect, way to use a structure.
What follows the . must be a field of a structure.Trying to use (parens) means that it is an expression to be evaluated.But an expression is not valid here, it must be a field-name.
This should be:
elevator->passenger = CAPACITY;

Related

This code has error at Status InitStack(Sqstack& s) i try to change & and its correct but adding more error again

This code has error at Status InitStack(Sqstack& s) and i dont know expected error at that section error: expected ';', ',' or ')' before '&' token
typedef int Status;
typedef struct Sqstack
{
elemtype* base;
elemtype* top;
int stacksize;
}Sqstack;
Status InitStack(Sqstack& s)
{
s.base=(elemtype*)malloc(inisize*sizeof(elemtype));
if (!s.base) exit(luber);
s.top=s.base;
s.stacksize=inisize;
return 1;
}

Why am I getting errors in my stack data structure?

I decided to make a stack using C. I just wrote the code for a push function and now I'm getting errors (it says something is wrong with my maxsize).
I f you ask me everything seems just right, and I don't have any idea on what would the error might be.
#include <stdio.h>
#include <stdlib.h>
#define maxsize 100;
int top=-1, item;
int s[maxsize];
void push()
{
if (top == maxsize-1)
{
printf("Stack overflow\n");
return;
}
top = top + 1;
s[top] = item;
}
The error that I am getting is:-
stack.c:3:20: error: expected ']' before ';' token
#define maxsize 100;
^
stack.c:5:7: note: in expansion of macro 'maxsize'
int s[maxsize];
^~~~~~~
stack.c: In function 'push':
stack.c:3:20: error: expected ')' before ';' token
#define maxsize 100;
^
stack.c:9:16: note: in expansion of macro 'maxsize'
if (top == maxsize-1)
^~~~~~~
stack.c:9:8: note: to match this '('
if (top == maxsize-1)
^
stack.c:16:5: error: 's' undeclared (first use in this function)
s[top]=item;
^
stack.c:16:5: note: each undeclared identifier is reported only once
for each function it appears in
#define directives do not end with a ;. Change to #define maxsize 100
Use this code:
#include <stdio.h>
#include <stdlib.h>
#define maxsize 100
int top=-1,item;
int s[maxsize];
void push()
{
if (top == maxsize-1)
{
printf("Stack overflow\n");
return;
}
top = top + 1;
s[top] = item;
}
Here you used ; after the #define statement which caused the problem.

Why does the compiler keep giving "error: expected identifier or '(' before ')' token"?

So I'm new to programming and specifically C programming and whilst I was making a program that would take data about an employ as input and put forth a modified salary, I ran into this error:
gcc /tmp/QbXFmvH0nL.c -lm
/tmp/QbXFmvH0nL.c:25:20: error: expected identifier or '(' before ')' token
25 | struct Emp_Dat_Inp() {
| ^
The error arises when I define the function. The code is this:
#include <stdio.h>
typedef struct Emp_Data{ //defines a struct for employee data, instead of
int Emp_Salary; //defining three seperate instances of variables
int leaves;
int Ext_Sal;
} empdat;
struct Emp_Data Emp_Dat_Inp();
//void Sal_Mod();
//void Tot_Sal();
int main()
{
empdat Emp_1;
Emp_1 = Emp_Dat_Inp();
/* Sal_Mod();
Tot_Sal();
*/
}
struct Emp_Dat_Inp() {
empdat Data;
while(Data.Emp_Salary > 70000 || Data.Emp_Salary < 30000 )
{
printf("Please enter the employee salary:\n");
printf("(NOTE: it must be within 30000 and 70000)");
scanf("%d", &Data.Emp_Salary);
}
printf("Please enter the extra sales made:\n");
scanf("%d", &Data.Ext_Sal);
printf("Finally please enter the number of leaves taken:\n");
scanf("%d", &Data.leaves);
return Data;
}
What I can't understand is why is it asking for an identifier or '(' when I have put the left bracket?
Maybe it has something to do with the struct that I'm using?

warning: implicit declaration of function ‘colcheck’ [-Wimplicit-function-declaration]

I am new to C programming which's why I am confused with its syntax.
Question: A Sudoku puzzle uses a 9 × 9 grid in which each column and row, as well as
each of the nine 3 × 3 subgrids, must contain all of the digits 1 ⋅ ⋅ ⋅ 9. Figure
4.26 presents an example of a valid Sudoku puzzle. This project consists of
designing a multithreaded application that determines whether the solution
to a Sudoku puzzle is valid.
There are several different ways of multithreading this application. The one
suggested strategy is to create threads that check the following criteria:
• A thread to check that each column contains the digits 1 through 9
• A thread to check that each row contains the digits 1 through 9
#include <stdlib.h>
int i,j,m,b,k;
void main()
{
int a[9][9]={1,2,3,4,5,6,7,8,9,
4,5,6,7,8,9,1,2,3,
7,8,9,1,2,3,4,5,6,
2,3,4,5,6,7,8,9,1,
5,6,7,8,9,1,2,3,4,
8,9,1,2,3,4,5,6,7,
3,4,5,6,7,8,9,1,2,
6,7,8,9,1,2,3,4,5,
9,1,2,3,4,5,6,7,8};
if(rowcheck(a)==1 && colcheck(a)==1 & cubecheck(a)==1)
{
printf("Success");
}
else{
printf("Failed");
}
}
int rowcheck(int a[9][9])
{
int c[10]={0};
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
c[a[i][j]]++;
}
for(k=1;k<=9;k++)
if(c[k]!=1)
{
printf("The value %d came %d times in %d row \n",k,c[k],i+1);
return 0;
}
for(k=1;k<=9;k++)
c[k]=0;
}
return 1;
}
int colcheck(int a[9][9])
{
int c[10]={0};
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
c[a[i][j]]++;
}
for(k=1;k<=9;k++)
if(c[k]!=1)
{
printf("The value %d came %d times in %d column \n",k,c[k],i+1);
return 0;
}
for(k=1;k<=9;k++)
c[k]=0;
}
return 1;
}
int cubecheck(int a[9][9])
{
int c[10]={0},count=0;
for(m=0;m<9;m+=3)
{
for(b=0;b<9;b+=3)
{
for(i=m;i<m+3;i++)
{
for(j=b;j<b+3;j++)
{
c[a[i][j]]++;
}
}
count++;
for(k=1;k<=9;k++)
if(c[k]!=1)
{
printf("The value %d came %d times in %d box\n",k,c[k],count);
return 0;
}
for(k=1;k<=9;k++)
c[k]=0;
}
}
return 1;
}```
I am getting this error plz help.
```proj1.c: In function ‘main’:
proj1.c:18:8: warning: implicit declaration of function ‘rowcheck’ [-Wimplicit-function-declaration]
if(rowcheck(a)==1 && colcheck(a)==1 & cubecheck(a)==1)
^~~~~~~~
proj1.c:18:26: warning: implicit declaration of function ‘colcheck’ [-Wimplicit-function-declaration]
if(rowcheck(a)==1 && colcheck(a)==1 & cubecheck(a)==1)
^~~~~~~~
proj1.c:18:43: warning: implicit declaration of function ‘cubecheck’ [-Wimplicit-function-declaration]
if(rowcheck(a)==1 && colcheck(a)==1 & cubecheck(a)==1)
^~~~~~~~~
proj1.c: At top level:
proj1.c:136:1: error: expected identifier or ‘(’ before ‘}’ token
}```
^
You need to give declarations for your functions before main() method since you define them afterwards:. A forward function declaration just tells the compiler that, somewhere in your code, a function with the same name will be defined, in this case after its usage in main().
Usually, in C/C++ you define function prototypes in header files, then include them in your main source code, so the functions can safely be defined after they are called.
#include <stdio.h>
#include <stdlib.h>
// add forward declarations for your functions here
int rowcheck(int [][9]);
int colcheck(int [][9]);
int cubecheck(int [][9]);
int main() {
// your code
return 0:
}
// method definitions afterwards
int rowcheck(int a[9][9]) {
// your definition here
}
int colcheck(int a[9][9]) {
// your definition here
}
int cubecheck(int a[9][9]) {
// your definition here
}
Also, define your main method as int main() rather than void main().

Error with expected operator before token

The error is telling me I'm missing an operator somewhere, but I simply cannot see it so I figured some fresh eyes could help me find it.
Code snippet:
static int min_val, max_val;
struct arrNum
{
int charged;
int count;
};
static struct arrNum nums[];
static int max_num = 0;
static void sort_order(int iNum)
{
if (iNum < 0)
return;
if (iNum > max_num)
max_num = iNum;
struct arrNum nums[iNum].charged = 1;
struct arrNums nums[iNum].count++;
return;
}
Errors:
mergeSort.c: In function 'sort_order':
mergeSort.c:32:29: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
struct arrNum nums[iNum].charged = 1;
^
mergeSort.c:32:29: error: expected expression before '.' token
mergeSort.c:33:30: error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token
struct arrNums nums[iNum].count++;
^
mergeSort.c:33:30: error: expected expression before '.' token
Any help is welcome. Thank you!
here is a version of the code with comments:
// following two statements will cause the compiler to raise
// warning messages because this statements
// declare the variables, but they are never used
static int min_val;
static int max_val;
// define a struct with two fields
struct arrNum
{
int charged;
int count;
};
// declare an instance of a pointer to a struct
// actually want an array of `struct arrNum`
// so need a number between the '[' and ']'
// it must be given a size that is at least 1 greater
// than the highest value of 'iNum'
static struct arrNum nums[];
static int max_num = 0;
// following line will raise a compiler warning
// because 'static' function can only be referenced in the current file
// and nothing in the posted code calls it.
static void sort_order(int iNum)
{
if (iNum < 0)
return;
if (iNum > max_num)
max_num = iNum;
// note: both following statements are
// writing into 'la la land'
// because all that has been declared for
// 'nums[]' is a pointer
// and because it is declared 'static'
// is initialized to 0
// so executing either of these statements
// will result in a seg fault event
nums[iNum].charged = 1; // nums[] already declared, so don't declare it again
nums[iNum].count++; // nums[] already declared, so don't declare it again
}

Resources