I don't understand this mistake about #pragma - c

This is the first part of my code and I can't understand the mistake written at the end of the message. Maybe I don't know enough to understand it. I would be very grateful if someone can explain me how #pragma works and what means the mistake. Please!!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//estructura
#pragma pack(push, 2)
typedef struct {
char telefono[10];
char nombre[26];
char direccion[30];
int cod_cont;
}Agenda;
#pragma pack(pop)
D:/msys64/mingw64/include/vadefs.h:14:9: error: expected '=', ',', ';', 'asm' or '__attribute__' before '#pragma'
14 | #pragma pack(push,_CRT_PACKING)
| ^~~~
I tried adding this parts:
#pragma pack(push, 2)
#pragma pack(pop)
but the mistake keeps popping up

Related

C header files without #ifndef still work...why? [duplicate]

This question already has answers here:
Why are typedef identifiers allowed to be declared multiple times?
(3 answers)
Closed 5 years ago.
Why don't I get the error messages?
I have three header files named DataType.h, printInt.h, printStr.h, and one myApp.c.
DataType.h
typedef int Integer;
typedef char String;
printInt.h
#include "DataType.h"
void printInt(Integer);
printInt.c
#include "printInt.h"
#include <stdio.h>
void printInt(Integer number){
printf("%d\n", number);
}
printStr.h
#include "DataType.h"
void printStr(String*);
printStr.c
#include "printStr.h"
#include <stdio.h>
void printStr(String *str){
printf("%s\n", str);
}
myApp.c
#include "printStr.h"
#include "printInt.h"
Integer main(void){
printInt(20);
printStr("hello");
return 0;
}
Clearly, I have included the DataType.h twice, and I did not use #ifndef to avoid redefinition of Integer and String. Please, someone, tell me how I can get the error messages to demonstrate the directive is working properly.
#ifndef __DATATYPE_H
#define __DATATYPE_H
typedef int Integer;
typedef char String;
#endif
Regardless having #ifndef or not, the gcc complier (version 5.4.0) does not generate any error messages. What's wrong?
Definitions of typedef and prototypes of functions can occur as many times as you want. For example:
typedef int lala;
typedef int lala;
void somePrototype();
void somePrototype();
int main() {
return 0;
}
will compile just fine: https://ideone.com/4EjfaR
Try adding the definition of a function to a header file. You will see then that you get a redefinition error and will require a header guard.

expected ')' before '*' token, can't seem to find error

So whenever I try to run my Makefile on my server, it always gives me the error is "Memory.c: 9 error: expected ')' before '*' token. But when I try to run it on my own computer, it works just fine. I've been trying to figure out what is wrong but can't seem to find it.
I've attached the 3 files that are used in this part of my program. Memory.c, Memory.h and ProcessInput.h.
This is Memory.c
/* Initializes memory */
#include <stdio.h>
#include <stdlib.h>
#include "memory.h"
void initializeMemory(memory** memArray, int memSize)
{
// Allocating space for memory array
*memArray = malloc(memSize * sizeof(memory));
if(*memArray == NULL)
{
fprintf(stderr, "Error allocating space for array of memory" );
exit(1); // exit(1) = Unsuccessful exit
}
// Initializing the contents within memory array
int i = 0;
for(i = 0; i < memSize; i ++)
{
((*memArray)[i]).occupied = false;
}
}
and this is Memory.h
// Definitions for Memory.c
#define bool int
#define true 1
#define false 0
#include "ProcessInput.h"
// Include guards to prevent redefinition of struct
#ifndef MEMORY_H
#define MEMORY_H
typedef struct memoryDetail
{
process process;
bool occupied;
} memory;
#endif
// Function declaration for memory.c
void initializeMemory(memory** memArray, int memSize);
the only thing used from ProcessInput.h is the process structure defined in ProcessInput.h
This is ProcessInput.h
// Include guards to prevent redefinition of struct
#ifndef PROCESSDETAIL_H
#define PROCESSDETAIL_H
typedef struct processDetail
{
int timeCreated;
int processID;
int memorySize;
int jobTime;
} process;
#endif
// function declarations for ProcessInput.c
void processInput(int* maxSize, int* count, process** processes, char* fileName);
I'm not too sure why it's giving me the error. I don't know where I'm supposed to be putting a missing right brace. Any advice is much appreciated!
edit: As informed, these are the following questions that I looked at but to not avail.
error: expected ‘)’ before ‘*’ token
Multiple of same error while compiling "error: expected ')' before '*' token
http://www.dreamincode.net/forums/topic/288956-error-expected-before-token/
thanks everyone for the help!
#include "memory.h" is different to #include "Memory.h" (i.e. C is case sensitive)
If you tried #include "myfile.h" instead of #include "MyFile.h" the error may be more obvious. In this case it just happens that the compiler finds the system memory.h.
<memory.h> is a header from C library of pre-standard era. It is quite possible that your standard library still provides it and the compiler takes that one instead of yours.
Try renaming your header file and see if it changes anything.

C gcc expected '=', ',', ';', 'asm' or '_attribute__' before

I'm very new to structs in C and I'm trying to write a program using a couple of structs and a main .c file. I'm trying to create a Create in the struct that returns a pointer to the struct for my main file to deal with.
However, every time I get the following error:
expected '=', ',', ';', 'asm' or '_attribute__' before 'createCust'
Customer createCust(...) { (with the carat at the beginning of createCust).
here are the relevant code snippets:
customer.h
#ifndef CUSTOMER_H
#define CUSTOMER_H
#include "order.h"
typedef struct customer *Customer;
Customer createCust(...);
void addOrder(...);
#endif
And customer.c:
#include "customer.h"
#include "order.h"
#include <stdio.h>
#include <string.h>
Customer createCust(...);
struct customer {
int customerNum;
char name[20];
order orders[20];
int index;
int capacity;
}
Customer createCust(int id, char nam[]) {
Customer c = malloc(sizeof(struct customer));
// other stuff
return c;
}
I am also getting another error about how order is an unknown type despite having included order.h in my file.
#ifndef ORDER_H
#define ORDER_H
typedef struct order *Order;
Order create(...);
#endif
You're missing a semicolon at the end of the struct's declaration, i.e.:
struct customer {
int customerNum;
char name[20];
order orders[20];
int index;
int capacity;
};

error: expected identifier or '(' before 'typedef'|

i'm working since a few weeks on the same project and never had this kind or error.
I got it now without touching to the concerned file, which is down there:
#ifndef DIJKSTRA_H_INCLUDED
#define DIJKSTRA_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define INFINI 100000.0
typedef struct dij
{
int ordre;
float** adjacencePoids;
float* l;
int* pred;
}t_dij;
int choix_action();
int choix_sommet_depart();
int choix_sommet_arrivee();
t_dij* allouer_dijkstra();
t_dij* allouer_dijktra_durees();
t_dij* dijkstra();
void afficher_resultat();
void sauver_resultat();
void detruire_struc();
#endif // DIJKSTRA_H_INCLUDED
This code gives me this error (Compiler is MinGW)
Line 11 error: expected identifier or '(' before 'typedef'|
Thanks!
I suspect the error lies in the file the header shown is included from, or in a file included just before this one.

Compilation errors I can't figure out

I have a piece of code that I used to run without problems. But now I'm going back to it and I just can't even compile it!
The piece that is not compiling is a .c file and I think it's complaining about libraries.
I try to compile it by doing this:
gcc f.c
And I get:
In file included from /usr/include/machine/ansi.h:39:0,
from /usr/include/sys/ansi.h:35,
from /usr/include/stdio.h:42
from f.c:7:
/usr/include/machine/int_types.h:45:1: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'typedef'
In file included from /usr/include/sys/inttypes.h:39:0,
from /usr/include/inttypes.h:36,
from /usr/include/netdb.h:98,
from f.c:9:
/usr/include/sys/stdint.h:39:18: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'int8_t'
In file included from /usr/include/ara/inet.h:69:0,
from netlib.h:7,
from f.c:16:
/usr/include/netinet/in.h:242:2: error: expected specifier-qualifier-list before '__int8_t'
/usr/include/netinet/in.h:259:2: error: expected specifier-qualifier-list before '__int8_t'
And this is my .c file:
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/socket.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "netlib.h"
#include <stdlib.h>
#define MACHSIZE 50
#define BUFFSIZE 256
#define MAXCONN 100
#define MAXFILES 500
#define COMBUFF 200
struct Connection{
int conn;
in_addr_t ip;
int port;
int numFilesOpened;
};
typedef struct Connection Connection;
Connection connection[MAXCONN];
struct Files{
int fid;
long machine;
int conn;
};
typedef struct Files Files;
Files files[MAXFILES];
int filesOpened = 0;
int port = 15061;
int numCli = 0;
char command[COMBUFF];
char response;
int conn;
char buffer[BUFFSIZE];
...
There is more to it but I think it has to do with the libraries so you might be able to help me with this.
Can anybody see anything here?
Also, in case this is important, I'm doing this on NetBSD but I tried on FreeBSD and it's the same.
Very mysterious.
Your error messages like "from rfa_cli.c:7:" imply there are 6 lines of code before the "first" line "#include ..." of your C file . Maybe there is some issue with your editor that is hiding these lines - some stray line ending or something. Recommend try compiling with only the "#include ..." in your file to see if the first error still occurs. If is does, start over with a clean text file.
Further, you say you are compiling "f.c", but the error messages say "rfa_cli.c". Please elaborate.

Resources