Error with expected operator before token - c

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
}

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.

error: expected expression before ‘{’ token in pthread rw_lock

So i have this code of a node in an kinda of a data structure and the node has a read write lock
to initialize the lock i used pthread_rwlock_init before using pthread_rwlock_t rwlock = PTHREAD_RWLOCK_INITIALIZER;
the thing is for some reason it gives an error saying that it was expected an expression before the PTHREAD_RWLOCK_INITIALIZER and i dont understand what it is because i know im not missing any ;
Code:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "state.h"
#include <pthread.h>
typedef struct inode_t {
type nodeType;
union Data data;
// ******************NEW ATTRIBUTE THAT INLCUDES THE LOCK *******************************
pthread_rwlock_t lock;
} inode_t;
void inode_table_init() {
for (int i = 0; i < INODE_TABLE_SIZE; i++) {
inode_table[i].nodeType = T_NONE;
inode_table[i].data.dirEntries = NULL;
inode_table[i].data.fileContents = NULL;
// ****************** NEW *******************************
// Initializes the lock
pthread_rwlock_init(&inode_table[i].lock ,NULL);
inode_table[i].lock = PTHREAD_RWLOCK_INITIALIZER;
}
}
int inode_create(type nType) {
/* Used for testing synchronization speedup */
insert_delay(DELAY);
for (int inumber = 0; inumber < INODE_TABLE_SIZE; inumber++) {
if (inode_table[inumber].nodeType == T_NONE) {
inode_table[inumber].nodeType = nType;
pthread_rwlock_init(&inode_table[inumber].lock ,NULL);
inode_table[inumber].lock = PTHREAD_RWLOCK_INITIALIZER;
if (nType == T_DIRECTORY) {
/* Initializes entry table */
inode_table[inumber].data.dirEntries = malloc(sizeof(DirEntry) * MAX_DIR_ENTRIES);
for (int i = 0; i < MAX_DIR_ENTRIES; i++) {
inode_table[inumber].data.dirEntries[i].inumber = FREE_INODE;
}
}
else {
inode_table[inumber].data.fileContents = NULL;
}
return inumber;
}
}
return FAIL;
}
Well it doesnt matter what the function does what it matters is that im having a strange error in the lines that im initializing the locks
Error:
fs/state.c: In function ‘inode_table_init’:
fs/state.c:34:31: error: expected expression before ‘{’ token
inode_table[i].lock = PTHREAD_RWLOCK_INITIALIZER;
^
fs/state.c: In function ‘inode_create’:
fs/state.c:81:41: error: expected expression before ‘{’ token
inode_table[inumber].lock = PTHREAD_RWLOCK_INITIALIZER;
Well any help would be appreciated.
You don't need those assignments. From the POSIX spec:
In cases where default read-write lock attributes are appropriate, the macro PTHREAD_RWLOCK_INITIALIZER can be used to initialise read-write locks that are statically allocated. The effect is equivalent to dynamic initialisation by a call to pthread_rwlock_init() with the parameter attr specified as NULL, except that no error checks are performed.
You can only use it in a variable initialization, not an assignment. And since it's equivalent to the calls on the lines before, it's redundant even it worked.

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

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;

Referencing the values in pointers to arrays (c)

Note: Fixed (decription at bottom)
For some reason the following code:
(*p_to_array)[m_p->number_of_match_positions] = (*p_to_temp_array)[k];
where the types are:
match_pos_t (*p_to_array)[];
match_pos_t (*p_to_temp_array)[];
int number_of_match_positions;
int k;
BTW: match_pos_t is a struct:
typedef struct match_pos
{
char* string;
long match_position;
}match_pos_t;
causes a 'syntax error before '(' error'
This error does not occur if this code replaced with other code.
Could someone give me an idea of why this is causing a syntax error, and how I should fix this problem?
Entire relevant code:
typedef struct match_pos
{
char* string;
long match_position;
}match_pos_t;
typedef struct match_positions
{
int number_of_match_positions;
match_pos_t (*match_positions)[];
}match_positions_t;
typedef struct search_terms
{
int number_of_search_terms;
char* search_terms[];
}search_terms_t;
int BMH_string_search(char* search_string, char* file_string, match_positions_t* match_positions)
{
return 0;
}
int determine_match_pos(search_terms_t** s_terms, char* file, match_positions_t* m_p)
{
int i,j,k;
match_positions_t* temp_m_p;
i=0;
/* s_terms is a null terminated data structure */
while((*s_terms+i) != NULL)
{
for(j=0; j<(*s_terms+i)->number_of_search_terms; j++)
{
/* search for the string positions */
BMH_string_search((*s_terms+i)->search_terms[j], file, temp_m_p);
/* load out search positions into the return array */
if(temp_m_p->number_of_match_positions != 0)
{
int total_m_ps = m_p->number_of_match_positions + temp_m_p->number_of_match_positions;
m_p->match_positions = (match_pos_t (*)[])realloc(m_p->match_positions, sizeof(match_pos_t)*total_m_ps);
k = 0;
for( ; m_p->number_of_match_positions<total_m_ps; m_p->number_of_match_positions++)
{
(*(m_p->match_positions))[m_p->number_of_match_positions] = (*(temp_m_p->match_positions))[k];
k++;
}
}
free(temp_m_p);
}
i++;
}
return 0;
}
It appears I have been rather stupid. An extra set of parenthesis around the values being referenced does the trick (question code has been updated with fix):
Original:
(m_p->*match_positions)[m_p->number_of_match_positions] = (temp_m_p->*match_positions)[k];
Fixed:
(*(m_p->match_positions))[m_p->number_of_match_positions] = (*(temp_m_p->match_positions))[k];
If anyone has an explanation, though, about why the first is incorrect, rather than the second, it would be nice to hear though, as I thought that
object->*object2
was the same as
*(object->object2)
Is this correct or is there some c definitions that I am missing out on here?
I thought that object->*object2 was the same as *(object->object2)
No, in C, the . and -> operators expect an identifier as their right operand. The .* and ->* operators don't exist in C, you have to spell out *(structure.member) or *(structure_ptr->member) manually.

Resources