error line: 43 compiler: expected identifier or '(' before ' [ ' token - c

I have this problem with the line 43, I dont know why
if I dont write that line, the error does not appear, I've seen the code for a while and I havent found why it appears
the error is here
/*for(i=0;i<MAX_ESTACIONES;i++){
Estaciones[i].nobici=10; //the problem is this line
}
*/
and this is the code
#include<stdio.h>
#include<time.h>
#define MAX_ESTACIONES 10
#define MAX_CARACTERES 40
#define MIN_CARACTERES 20
typedef char tipodato;
typedef struct info
{
tipodato nombre[MAX_CARACTERES];
tipodato edad[MIN_CARACTERES];
tipodato sexo[MIN_CARACTERES];
tipodato curp[MIN_CARACTERES];
tipodato domicilio[MAX_CARACTERES];
tipodato nacimiento[MAX_CARACTERES];
tipodato comentario[MAX_CARACTERES];
tipodato contrasenia[MAX_CARACTERES];
int prestamo;
struct info *sig;
}Persona;
typedef struct
{
int nobici;
clock_t inicio,fin;
} Estaciones[MAX_ESTACIONES];
typedef Persona *Listapersona;
Listapersona L;
int main()
{
Persona *posicion;
Persona *P;
P=NULL;
posicion=NULL;
int opcion,salir,i;
salir=0;
for(i=0;i<MAX_ESTACIONES;i++){
Estaciones[i].nobici=10; //the problem is this line
}
return 0;
}

The typedef makes Estaciones the name of a type, and not a variable as you intended:
typedef struct
{
int nobici;
clock_t inicio,fin;
} Estaciones[MAX_ESTACIONES];
Remove the typedef to make it a variable, or make both a type and variable:
typedef struct
{
int nobici;
clock_t inicio,fin;
} TheStructName;
TheStructName Estaciones[MAX_ESTACIONES];

Remove the typedef in typedef struct ... Estaciones[...]. You are obviously trying to define Estaciones as a type and use it as a variable. Also, instead of using typedef struct { ... } Name;, you should really use struct Name { ... }; nowadays.

Related

How to fix incomplete type of struct

Basically I'm running this code on this CLion application due to my teacher's demands.
I have my struct of 'Estado' defined in .c file like this:
struct estado{
char modo;
char jogador;
char matriz[8][8];
int pretas;
int brancas;
};
And have this on my .h file:
typedef struct estado* Estado;
In my main.c file where i try to acess:
printf("%s",novo -> matriz[1]);
It says: "error : dereferencing pointer to incomplete type ‘struct estado' "
Can you help me ?
You should put the struct declaration in your .h file instead of the .c file.
If you don't define the struct in the header, you'll end up with an opaque pointer, so that whatever is inside that struct is hidden from other translation units.
You can always declare some accessor functions in the header
typedef struct estado Estado; // <- Not a pointer
Estado *alloc_estado(void);
void print_row(Estado *e, int i);
// ...
Define those in the .c file
#include "estado.h"
struct estado
{
char modo;
char jogador;
char matriz[8][8];
int pretas;
int brancas;
};
void print_row(Estado *e, int i)
{
printf("%s\n", e->matriz[i]);
}
// ...
And use in main
#include "estado.h"
int main(void)
{
Estado *pes = alloc_estado();
// ...
print_row(pes, 1);
// ...
free(pes);
}

Getting an error in .h file when trying to compile it. semi-colon missing

struct MemPidsData
{
int PID[7];
}
struct MemInfoData
{
int heater;
int pump;
int fan;
int temperature;
int valve;
}
#define MEMORY_NAME_INFO "InfoMem" /* Common Memmory */
#define MEMORY_SIZE_INFO sizeof(struct MemData)
#define MEMORY_SIZE_PIDS sizeof(struct MemData)
#define MEMORY_NAME_PIDS "CommonMem" /* Common Memmory */
I am getting an error that semicolon is missing on the line where struct MemInfoData is defined... Please help..
You actually have two missing semicolons - it should be:
struct MemPidsData
{
int PID[7];
}; // <<<
struct MemInfoData
{
int heater;
int pump;
int fan;
int temperature;
int valve;
}; // <<<
Always declare struct to end with ;
struct MemPidsData
{
int PID[7];
};

structure within structure declared in a file not able to access inner structure

I have created a file msgbuf.h which is as follows:
//msgbuf.h
typedef struct msgbuf1
{
long mtype;
M1 *m;
} message_buf;
typedef struct msgclient
{
int msglen;
int msgtype;
char cp[100];
}M1;
And a program as check.c. Below prog is giving error that there is no M1. Why is it so? What mistake am I doing?
I think the contents of file "msgbuf.h" should get copied in the prog check.c and the program should run fine. Please let me know this.
//check.c
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include"msgbuf.h"
int main()
{
message_buf *sbuf;
sbuf=malloc(sizeof(sbuf));
sbuf->m=malloc(sizeof(M1));
sbuf->m->msglen=10;
printf("\n%d",sbuf->m->msglen);
printf("\n %d",sizeof(sbuf->m));
return 0;
}
Thanks :)
Simple, declare M1 before message_buf; .
typedef struct msgclient
{
int msglen;
int msgtype;
char cp[100];
}M1;
typedef struct msgbuf1
{
long mtype;
M1 *m;
} message_buf;
And read the keltar's comments below the question too.
You should declare M1 before using it:
//msgbuf.h
typedef struct msgclient
{
int msglen;
int msgtype;
char cp[100];
}M1;
typedef struct msgbuf1
{
long mtype;
M1 *m;
} message_buf;

HiC : Structure within Structure?

this is my first time using structure within a structure.
I encounter this error when I compile my program.
error: field 'results' has incomplete type.
The error is referring to this line of code.
-->struct result_t results;
Any help please? :)
Thanks.
typedef struct {
char moduleCode[8];
char grade[3];
} result_t;
typedef struct {
char name[31];
struct result_t results;
} student_t;
Edit:
I changed my codes:
typedef struct {
char moduleCode[8];
char grade[3];
} result_t;
typedef struct {
char name[31];
result_t results;
} student_t;
and I got a new compilation error.
error : subscripted value is neither array nor pointer.
The line of code that triggered that error is as follows.
printf(" %-7s %-2s %d\n", student.results[i].module_code, student.results[i].grade, student.results[i].mc);
Result is not an array. you should either change the structure student with:
typedef struct {
char name[31];
result_t results[MAX_NUM_RESULTS];
} student_t;
Or change the printf to:
printf(" %-7s %-2s %d\n", student.results.module_code, student.results.grade, student.results.mc);
It depends on how many possible results one student may have.
since you are using typedef use this
typedef struct {
char name[31];
result_t results;<---remove struct
} student_t;

Instantiations for structs in C?

So I'm trying to learn C right now, and I have some basic struct questions I'd like to clear up:
Basically, everything centers around this snippet of code:
#include <stdio.h>
#include <stdlib.h>
#define MAX_NAME_LEN 127
const char* getName(const Student* s);
void setName(Student* s, const char* name);
unsigned long getStudentID(const Student* s);
void setStudentID(Student* s, unsigned long sid);
int main(void) {
Student sarah;
const char* my_name = "Sarah Spond";
setName(&sarah, my_name);
printf("Name is set to %s\n", sarah.name);
}
typedef struct {
char name[MAX_NAME_LEN + 1];
unsigned long sid;
} Student;
/* return the name of student s */
const char* getName (const Student* s) { // the parameter 's' is a pointer to a Student struct
return s->name; // returns the 'name' member of a Student struct
}
/* set the name of student s
If name is too long, cut off characters after the maximum number of characters allowed.
*/
void setName(Student* s, const char* name) { // 's' is a pointer to a Student struct | 'name' is a pointer to the first element of a char array (repres. a string)
int iStringLength = strlen(name);
for (i = 0; i < iStringLength && i < MAX_NAME_LEN; i++) {
s->name[i] = name[i];
}
}
/* return the SID of student s */
unsigned long getStudentID(const Student* s) { // 's' is a pointer to a Student struct
return s->sid;
}
/* set the SID of student s */
void setStudentID(Student* s, unsigned long sid) { // 's' is a pointer to a Student struct | 'sid' is a 'long' representing the desired SID
s->sid = sid;
}
However, when I try and compile the program, I get a bunch of errors saying that there's an "unknown type name Student". What am I doing wrong?
Thanks!
Move the type definition for Student - the typedef .. right after #define MAX_NAME_LEN 127, i.e. before it's being referenced.
You need to move the declaration of the Student struct above the first time it is referenced by other code - otherwise those functions will not know what it is.
Struct declarations need to be defined before you use them , so you need to move your Student
As cnicutar said, move the typedef - the reason for this is that the type must be known before it's used. Alternatively, you can forward declare the type.
> Move the typedef .. right after #define MAX_NAME_LEN 127, i.e. before
> it's being used.
OR, if you want to keep your definition after, and if you are ready to use a pointer to Student, you can:
#include <stdio.h>
#include <stdlib.h>
#define MAX_NAME_LEN 127
// forward declare Student ici
struct Student;
//...
// in main, use a pointer to student
int main(void) {
Student *sarah; // Changed to pointer
const char* my_name = "Sarah Spond";
setName(sarah, my_name); // Pass the pointer instead of reference
printf("Name is set to %s\n", sarah->name); // Use the pointer
//....
delete sarah; // delete object when done
}
// Change struct decl to the following // can't explain the diff yet
struct Student {
char name[MAX_NAME_LEN + 1];
unsigned long sid;
};
A basic structure of a C program is:
//======DOCUMENT SECTION=========
//File:test.c
//Author:
//Description:
//...
//================================
//====INCLUDE SECTION=============
#include "lib1"
#include <lib2>
//================================
//========DEFINITIONS SECTION=====
#define TRUE 1
#define FALSE 0
//================================
//========STRUCTURES SECTION======
struct P{
};
//================================
//========TYPEDEFS SECTION========
typedef *P P;
//================================
//========FUNCTION HEADERS========
void foo1(...);
int foo2(...,...,...);
//================================
//=========GLOBAL VARIABLES=======
int GLOBAL_INT;
float GLOBAL_FLOAT;
//================================
//=====MAIN FUNCTION DEFINITION===
void main(void)
{
...
...
...
}
//=================================
//======FUNCTIONS DEFINITION======
void foo1(...)
{
}
int foo2(...,...,...)
{
}
//================================
A main function is where a C program starts. A main function also typically has access to the command arguments given to the program when it was executed.
Usually you have got:
int main(void);
int main();
int main(int argc, char **argv);
int main(int argc, char *argv[]);

Resources