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.
Related
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define LEN_ID 3
#define LEN_P 30
#define LEN_CIDADE 50
#define AT 40
typedef struct aeroporto
{
char id[LEN_ID + 1];
char pais[LEN_P + 1];
char cidade[LEN_CIDADE + 1];
} Aeroporto;
int findavailablespot(Aeroporto l[AT])
{
int i = found = 0;
for (;i<AT;i++) {
if (l[i] = {"aaa","bbb","ccc"}) //Error in this line
break;
if (found)
return i;
else
return -1;
}
}
So i am creating the structure aeroporto then a vector made up of aeroportos and i want to check if {"aaa","bbb","ccc"} shows up inside the vector.
Help?
Sorry for the formatting, new at this
You have to use strcmp() to compare strings. There's no shortcut for doing this with all the members of a structure, you have to test each one individually and combine with &&.
You also forgot to set found before breaking out of the loop.
int i = 0, found = 0;
for (;i<AT;i++) {
if (strcmp(l[i].id, "aaa") == 0 && strcmp(l[i].pais, "bbb") == 0 && strcmp(l[i].cidade, "ccc")) {
found = 1;
break;
}
}
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().
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;
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
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does VS2010 give syntax errors when syntax is correct?
I'm trying to develop a kind of Windows 32 service in C language, using Visual Studio 2010.
I created a new project, and inserted .c files :
main.c
service.c
misc.c
I also have two header files :
myerrors.h
my.h
Here's the code I have (be aware that it's just a draft).
main.c :
#include <Windows.h>
#include <stdlib.h>
#include <stdio.h>
#include "stdafx.h"
#include "my.h"
#include "myerrors.h"
static int parse_args(int ac, char **av)
{
int i = 0;
while (++i < ac)
if (strcmp(av[i], "-i") && !InstallMyService())
return false;
else if (strcmp(av[i], "-d") && !UninstallMyService())
return false;
else if (strcmp(av[i], "-p"))
if (!av[i + 1])
return false;
else
{
if (!InsertPathInRegistry(av[i + 1]))
return false;
i++;
}
else
return false;
return true;
}
int main(int ac, char **av)
{
HANDLE hLogFile;
if ((hLogFile = CreateFile(LOG_FILE_PATH, GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL)) == INVALID_HANDLE_VALUE)
aff_error(CANT_CREATE_FILE);
if (ac > 1)
{
if (!parse_args(ac, av))
{
aff_error(BAD_ARGUMENTS);
return EXIT_FAILURE;
}
}
else
{
SERVICE_TABLE_ENTRY DispatchTable[] = {{DC_SERVICE_NAME, ServiceMain}, {NULL, NULL}};
StartServiceCtrlDispatcher(DispatchTable);
}
getchar();
if (!CloseHandle(hLogFile))
aff_error(CLOSE_FILE_FAILED);
return EXIT_SUCCESS;
}
misc.c :
#include <Windows.h>
#include <stdio.h>
#include "my.h"
#include "myerrors.h"
void aff_error(char *error_str)
{
fprintf(stderr, "ERROR: %s\n", error_str);
}
bool InsertPathInRegistry(char *path)
{
printf("LOG: Inserting %s as ", path);
}
void WriteInLogFile(HANDLE hLogFile, char *log_string)
{
printf("WriteInLogFile function");
}
service.c :
#include <Windows.h>
#include "my.h"
bool InstallMyService()
{
return true;
}
bool UninstallMyService()
{
return true;
}
void WINAPI ServiceCtrlHandler(DWORD Opcode)
{
}
void WINAPI ServiceMain(DWORD ac, LPTSTR *av)
{
}
My headers are just some function declarations and macros such as :
# define DC_SERVICE_NAME "MyService"
/* MISC functions */
void aff_error(char *error_str);
my.h
#ifndef _MY_H_
# define _MY_H_
#include <Windows.h>
#include <strsafe.h>
/* Macros */
# define LOG_FILE_PATH "c:\\my_log_file.txt"
# define DC_SERVICE_NAME "MyService"
/* MISC functions */
void aff_error(char *error_str);
/* SERVICE functions */
void WINAPI ServiceMain(DWORD ac, LPTSTR *av);
bool InstallMyService();
bool UninstallMyService();
bool InsertPathInRegistry(char *path);
void WINAPI ServiceCtrlHandler(DWORD Opcode);
#endif /*!MY_H_ */
While trying to compile the project, i got some weird errors :
my.h(19): error C2061: syntax error : identifier 'InstallMyService'
my.h(19): error C2059: syntax error : ';'
my.h(19): error C2059: syntax error : ')'
Or :
my.h(21): error C2061: syntax error : identifier 'InsertPathInRegistry'
my.h(21): error C2059: syntax error : ';'
my.h(21): error C2059: syntax error : 'type'
I checked on some forums that says those errors are commonly errors with includes badly placed, but I don't really know in this case, I don't think I made mistakes with includes...
Can anyone illuminate me ?
Thanks.
bool is not a data type in ANSI C. It is a data type in the C99 version of the language, only if <stdbool.h> is included, but Visual Studio does not support C99, only C89 (C99 also adds the _Bool data type, which can be used without including any headers).
I suggest you replace bool with another type such as int, or use a typedef to alias it with int or unsigned char or something.