I'm working with structs for the first time in a long time and I am having multiple issues. I'm just trying to systematically address them but this one in particular is giving me a hard time. When I try to make the program, I get this:
`datime.c:9:23:error: expected ‘;’, ‘,’ or ‘)’ before ‘.’ token bool
isConflict(Class.Time*, Class.Time*){`
and
`datime.c:18:21: error: expected ‘;’, ‘,’ or ‘)’ before ‘.’ token bool
isEarlier(Time.Start*, Time.Start*){`.
My file, schedule.h:
/*
* file: datime.h
*/
typedef struct
{ int hour;
int min;
} Start;
typedef struct
{ int hour;
int min;
} Stop;
typedef struct
{ struct Start;
struct Stop;
} Time;
typedef struct
{ struct Time;
int Days[7];
} Class;
bool isConflict(struct TimeA*, struct TimeB*);
bool isEarlier(struct TimeA*, struct TimeB*);
My file, schedule.c:
/*
* file: datime.c
*/
#include <stdio.h>
#include <stdbool.h>
#include "datime.h"
bool isConflict(Class.Time*, Class.Time*){
free(classA);
free(classB);
if ((classA.Start <= classB.Start) && (classA.Stop >= classB.Stop))
return 1;
else
return 0;
}
bool isEarlier(Time.Start*, Time.Start*){
free(classA);
free(classB);
if (classA.Start < classB.Start)
return 1;
else
return 0;
}
Also, the driver.c, being used just to test the functions and structs:
#include <stdbool.h>
#include "datime.h"
main()
{
void setClass(struct Class)
{ struct Class EE205{
EE205.Time.Start.hour = 8;
EE205.Time.Start.min = 30;
EE205.Time.Stop.hour = 9;
EE205.Time.Stop.min = 20;
// Initializing array to zero
memset(EE205.Days, 0, 7 * sizeof(Days[0]));
EE205.Days[1] = 1;
EE205.Days[3] = 1;
EE205.Days[5] = 1;
};
struct Class EE367{
EE367.Time.Start.hour = 10;
EE367.Time.Start.min = 30;
EE367.Time.Stop.hour = 11;
EE367.Time.Stop.min = 20;
// Initializing array to zero
memset(EE367.Days, 0, 7 * sizeof(Days[0]));
EE367.Days[1] = 1;
EE367.Days[3] = 1;
EE367.Days[5] = 1;
};
struct Class EE315{
EE315.Time.Star.hour = 12;
EE315.Time.Start.min = 30;
EE315.Time.Stop.hour = 13;
EE315.Time.Stop.min = 20;
// Initializing array to zero
memset(EE315.Days, 0, 7 * sizeof(Days[0]));
EE315.Days[1] = 1;
EE315.Days[3] = 1;
EE315.Days[5] = 1;
};
if(isConflict(EE315, EE367))
printf("Scheduling conflict! EE315 and EE367 conflict.");
if(isConflict(EE315, EE205))
printf("Scheduling conflict! EE315 and EE205 conflict.");
if(isConflict(EE205, EE367))
printf("Scheduling conflict! EE205 and EE367 conflict.");
}
There are a lot of errors, but these errors are the two that I cannot move passed for whatever reason. Thanks in advance.
Allen
This isn't any legal C syntax.
bool isConflict(Class.Time*, Class.Time*)
(Actually almost nothing of the code is valid. I suggest you start learning structs and pointers with a much smaller example/program)
You'll have to declare it as e.g.
bool isConflict(Class* classA, Class *classB){
The body is quite strange too, I cannot really figure out what you intend to do:
free(classA); <--- free() classA/classB, but 2 lines down you try to access them ?
free(classB);
// if ((classA.Start <= classB.Start) && (classA.Stop >= classB.Stop))
//since classA/classB are pointers you need -> to access their members:
// You also need to compare the members of Time, you can't compare structs
// in C, e.g. to check the hours:
if ((classA->Start.hour <= classB->Start.hour) &&
(classA->Stop.hour >= classB->Stop.hour))
return 1;
else
return 0;
}
Your isEearlier function has the same problem.
main()
{
//you can't declare a function within another function like this.
//what is the purpose of the setClass function ? There is no code that calls this function
//you also need to give names to your function parameters, if you want a pointer, it'd be
//void setClass(struct Class *my_class)
void setClass(struct Class)
{
Your Time struct is wrong, you havn't given any variable names to the members, it should be:
typedef struct
{ Start Start;
Stop Stop;
} Time;
(However, your struct Start and struct Stop are identical, there's no need to create 2 struct definitions for both)
The initializers for your struct is wrong:
struct Class EE205{
EE205.Time.Start.hour = 8;
EE205.Time.Start.min = 30;
EE205.Time.Stop.hour = 9;
EE205.Time.Stop.min = 20;
};
Note that you say struct Class, but nowhere have you defined a struct Class. You have a typedef
to a struct, the typedef is just called Class, so the compiler will not know about struct Class, it only knows about Class
You can set the members like:
Class EE205;
EE205.Time.Start.hour = 8;
EE205.Time.Start.min = 30;
EE205.Time.Stop.hour = 9;
EE205.Time.Stop.min = 20;
// Initializing array to zero
memset(EE205.Days, 0, 7);
EE205.Days[1] = 1;
EE205.Days[3] = 1;
EE205.Days[5] = 1;
Or initialize it:
Class EE205 = {
{
{8, 30},
{9,20}
},
{0,1,0,1,0,1}
};
This is completely wrong, you cannot use structure type as structure members.
/* 2 structures type doing the same thing? */
typedef struct
{ int hour;
int min;
} Start;
typedef struct
{ int hour;
int min;
} Stop;
typedef struct
{ struct Start; /* variable name missing */
struct Stop;
} Time;
typedef struct
{ struct Time;
int Days[7];
} Class;
bool isConflict(struct TimeA*, struct TimeB*); /* makes no sense */
bool isEarlier(struct TimeA*, struct TimeB*);
Let's rewrite it:
/* Replaced Start/Stop with time */
typedef struct
{ int hour;
int min;
} Time;
/*
Added member names.
Removed struct because using typedef for Time
*/
typedef struct
{ Time start;
Time stop;
} TimeSpan;
typedef struct
{ TimeSpan timeSpan;
int Days[7];
} Class;
/*
First type, then variable name for each parameter.
Also removed the invalid struct marker.
*/
bool isConflict(Time * a, Time * b);
bool isEarlier(Time * a, Time * b);
This does't fix the problems with the actual code yet, but now you should
at least have your type declarations right.
You have multiple errors.
You are using typedef so there is not need to use struct after
You just enter the type in the struct and you forgot the name
Same in the function declaration
Here is your header file:
/*
* file: datime.h
*/
typedef struct
{ int hour;
int min;
} Start;
typedef struct
{ int hour;
int min;
} Stop;
typedef struct
{ Start start;
Stop stop;
} Time;
typedef struct
{ Time time;
int days[7];
} Class;
bool isConflict(Time *timeA, Time *timeB);
bool isEarlier(Time *timeA, Time *timeB);
In your source file there is mistakes too:
You are using struct pointer so to acces member you have to use -> and not .
You are using bool with <stdbool.h> so you should return true or false
You can't free things that you will access later (or you will have a segmentation fault)
Here is your source file:
/*
* file: datime.c
*/
#include <stdio.h>
#include <stdbool.h>
#include "datime.h"
bool isConflict(Time *timeA, Time *timeB) {
if ((timeA->start <= timeB->start) && (timeA->stop >= timeB->stop))
return true;
else
return false;
}
bool isEarlier(Time *timeA, Time *timeB) {
if (timeA->start < timeB->start)
return true;
else
return false;
}
There is also a lot of mistakes in your main. I let you see the other responses ....
Related
I've one solid structure and another structure with pointers. The purpose of program is to assign: solid structure to structure with pointers and access each solid structure member using other structure pointer.
I've problem statement: as two structure member as not symmetric, when i assign solid structure address to structure with pointers, member pointer initialization go bad and crash the system.
Does anyone have any approach to find a solution for this problem in an optimized way?
----------------------------------------------------------------------- program -----------------------------
#include <stdio.h>
#include <string.h>
#include <stddef.h>
/* ===================== Binding Structure ================================= */
typedef struct
{
char id;
}tmodel;
typedef struct
{
char id;
}tbrand;
typedef struct
{
char id;
}tcommercialRef;
typedef struct
{
char id;
}tserialnum;
typedef struct
{
tmodel *smodel;
tbrand *sbrand;
tcommercialRef *scommref;
tserialnum *sslnum;
}tmetadata;
typedef struct
{
tmetadata *smetadata;
}tlink;
typedef struct
{
tlink *slink;
}trefernce;
typedef struct
{
char id[10];
int ttl;
int tss;
trefernce *sref;
}telectrical;
/* ===================== Application Strucuture ==============================*/
void filldata(telectrical *elec);
typedef struct
{
tmodel smodel;
tbrand sbrand;
tcommercialRef scommref;
tserialnum sslnum;
}Ymetadata;
typedef struct
{
Ymetadata smetadata;
}slink;
typedef struct
{
slink glink;
}refernce;
typedef struct
{
char id[10];
int ttl;
int tss;
refernce grefernce;
}gtelectrical;
//solid strucutre object
gtelectrical obj;
//structure pointer object
telectrical *elec = {0};
/* =============================== main.c =================================== */
int main()
{
printf("test");
//static void **p = (void *)&elec;
obj.tss = 55;
obj.ttl = 100;
obj.grefernce.glink.smetadata.smodel.id = 5;
obj.grefernce.glink.smetadata.sbrand.id = 6;
obj.grefernce.glink.smetadata.scommref.id = 7;
obj.grefernce.glink.smetadata.sslnum.id = 8;
elec = (telectrical *)&obj;
//elec structure -> sref pointer goes bad as it's not same type as "grefernce"
//*p = (void *)&obj;
//static long x = (long) offsetof( telectrical, sref);
//(long) offsetof(struct telectrical, sref);
//*(*p + x) = obj.grefernce.glink.smetadata.;
elec->id[0] = 0;
elec->id[1] = 1;
elec->id[2] = 2;
elec->ttl = 5;
elec->tss = 10;
elec->sref->slink->smetadata->sslnum->id = 4;
elec->sref->slink->smetadata->sbrand->id = 1;
elec->sref->slink->smetadata->scommref->id = 2;
elec->sref->slink->smetadata->smodel->id = 3;
//filldata(elec);
printf("------");
printf("%d\n",elec->sref->slink->smetadata->sslnum->id);
printf("%d\n",elec->sref->slink->smetadata->sbrand->id);
printf("%d\n",elec->sref->slink->smetadata->scommref->id);
printf("%d\n",elec->sref->slink->smetadata->smodel->id);
return 0;
}
/* //////////////////////////////////////// user scope ////////////////////////////// */
void filldata(telectrical *pelec)
{
pelec->id[0] = 0;
pelec->id[1] = 1;
pelec->id[2] = 2;
pelec->ttl = 5;
pelec->tss = 10;
//pelec->sref->slink->smetadata->sslnum->id = 4;
//pelec->sref->slink->smetadata->sbrand->id = 1;
//pelec->sref->slink->smetadata->scommref->id = 2;
//pelec->sref->slink->smetadata->smodel->id = 3;
}
You are not assigning memory for the pointers to other struct present inside another struct. Here is something which might help you in multi-level memory allocation and assignment:
#include<stdio.h>
#include<stdlib.h>
typedef struct A
{
int i;
}A_Node;
typedef struct B
{
A_Node *A_ptr;
}B_Node;
typedef struct C
{
B_Node *B_ptr;
}C_Node;
int main(void)
{
//ACCESSING-MANIPULATING A USING B
B_Node B_obj;
B_obj.A_ptr=malloc(sizeof(*(B_obj.A_ptr)));
(B_obj.A_ptr)->i=192;
A_Node A_obj=*(B_obj.A_ptr); //For checking if the allocation is successful and good
printf("%d\n",A_obj.i);
//ACCESSING-MANIPULATING A USING C
C_Node C_obj;
C_obj.B_ptr=malloc(sizeof(*(C_obj.B_ptr))); //allocating space for struct of B using C object
(C_obj.B_ptr)->A_ptr = malloc(sizeof(*((C_obj.B_ptr)->A_ptr))); //allocating space for struct of A using B Struct for which space was allocated in previous step by C struct
((C_obj.B_ptr)->A_ptr)->i=876;
A_obj=*((C_obj.B_ptr)->A_ptr); //For checking if the allocation is successful and good
printf("%d\n",A_obj.i);
return 0;
}
Read the code and ask if there are any doubts, in the similar way this multi-level struct-inside-struct can be created (though it would be ugly).
Well I am wanting to change the way my structures are written, currently I use array and I need to limit its use, but I wanted a way to create a dynamic array that is the size of the reading done, without always having to edit the array value.
Current Code:
struct sr_flag {
int value_flag;
};
struct er_time {
int value_time;
};
struct se_option {
struct sr_flag flag[50];
struct er_time time[50];
};
struct read_funcs
struct se_option *option;
void (*option_func) (void);
...
}
struct read_funcs func_;
struct read_funcs *func;
int sr_flags(int i, int fg, int val) {
if(i < 0)
return 0;
return func->option[i].flag[fg].value_flag = val;
}
void option_func(void) {
struct se_option fnc;
fnc.option = malloc(500 * sizeof(*(fnc.option)));
}
void read_fnc() {
func = &func_;
func->option = NULL;
func->option_func = option_func;
}
I look for a way to remove the array amount [50] instead each time the sr_flags function is executed the limit is raised
Example: sr_flags function executed 1x array would be [1] if executed 2x would be [2]
I also think about doing the same with the option_func function
I tried using the following more unsuccessfully
struct se_option {
struct sr_flag *flag;
struct er_time time[50];
};
int sr_flags(int i, int fg, int val) {
if(i < 0)
return 0;
func->option[i].flag = malloc(1 * sizeof(*(func->option[i].flag)));
return func->option[i].flag[fg].value_flag = val;
}
int main () {
for(int i < 0; i < 10; i++)
sr_flags(i, 1, 30);
return 0;
}
I'm not 100% certain on what it is you want but I think you just want to call realloc and increase the size by the amount you provide. And that's very easy to do, as for the values you want with the arrays I'm not sure so I just used a placeholder value.
#include <stdio.h>
#include <stdlib.h>
struct sr_flag {
int value_flag;
};
struct er_time {
int value_time;
};
struct se_option {
struct sr_flag* flag;
struct er_time* time;
};
void allocateflags(struct se_option* options, int size, int val){
options->flag = realloc(options->flag, size*sizeof(struct sr_flag));
struct sr_flag* flag = options->flag+size-1;
flag->value_flag = val;
}
void allocatetime(struct se_option* options,int size, int val){
options->time = realloc(options->time, size*sizeof(struct er_time));
struct er_time* time = options->time+size-1;
time->value_time = val;
}
void displayflagvalues(struct se_option* options,int size){
for(int index = 0; index < size ; ++index){
printf("flag: %i\n",options->flag[index].value_flag);
}
}
void displaytimevalues(struct se_option* options, int size){
for(int index = 0; index < size ; ++index){
printf("time: %i\n",options->time[index].value_time);
}
}
int main(){
struct se_option options = {0};
for(int index = 0; index < 10; ++index){
allocateflags(&options, index,index);
allocatetime(&options, index,index);
}
displayflagvalues(&options, 10);
displaytimevalues(&options,10);
return 0;
}
The code creates an se_option structure wheren sr_flag and er_time pointers are null. Then there's two functions one allocateflags and the other allocatetime, both of which call realloc with the size you provide. When you call realloc, all previous memory is copied over to the new array. Also free is called automatically by realloc.
This step
struct sr_flag* flag = options->flag+size-1;
flag->value_flag = val;
struct er_time* time = options->time+size-1;
time->value_time = val;
Is slightly redundant but it was just to show the newest array can hold the value. If you understand pointer arithmetic, all its doing is incrementing the pointer to the last position then subtracting 1 struct size and setting that value. Basically setting the value of the final array in the pointer.
I am writing a sequence linked list in C language.
Execution result: Process returned 255 (0xFF) execution time 2.144s
The struct contains array component and use 'typedef' struct as pointer type, what's wrong with it, can anyone help me?
#include <stdio.h>
#define OK 1
#define ERROR 0
#define ERROR_EXIT -1
#define MAXSIZE 30
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType data[MAXSIZE];
int len;
}*SQLIST;
Status listInit(SQLIST L)
{
int len = 10;
int i = 0;
for(;i<len;i++)
{
L->data[i] = i; // There is problem here
}
L->len = 10;
return OK;
}
Status listShow(SQLIST L)
{
return OK;
}
int main()
{
SQLIST L;
listInit(L);
printf("%d\n",L->len);
return OK;
}
You should define the struct as:
typedef struct
{
ElemType data[MAXSIZE];
int len;
} SQLIST, *PSQLIST;
Now, you can allocate L in main() like so:
PSQLIST L = malloc(sizeof(SQLIST));
Don't forget to free(L) when you're done with it, and rename all your current instances of SQLIST to PSQLIST.
I'm doing an assignment for my data structures class and I have very little experience with C structures and C in general.
This is the .h file that I was given to do the assignment:
#ifndef C101IntVec
#define C101IntVec
typedef struct IntVecNode* IntVec;
static const int intInitCap = 4;
int intTop(IntVec myVec);
int intData(IntVec myVec, int i);
int intSize(IntVec myVec);
int intCapacity(IntVec myVec);
IntVec intMakeEmptyVec(void);
void intVecPush(IntVec myVec, int newE);
void intVecPop(IntVec myVec);
#endif
This is the .c implementation that I've made:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "intVec.h"
typedef struct IntVecNode {
int* data;
int sz; // Number of elements that contain data
int capacity; // How much is allocated to the array
} IntVecNode;
typedef struct IntVecNode* IntVec;
//static const int intInitCap = 4;
int intTop(IntVec myVec) {
return *myVec->data;
}
int intData(IntVec myVec, int i) {
return *(myVec->data + i);
}
int intSize(IntVec myVec) {
return myVec->sz;
}
int intCapacity(IntVec myVec) {
return myVec->capacity;
}
IntVec intMakeEmptyVec(void) {
IntVec newVec = malloc(sizeof(struct IntVecNode));
newVec->data = malloc(intInitCap * sizeof(int));
newVec->sz = 0;
newVec->capacity = intInitCap;
return newVec;
}
void intVecPush(IntVec myVec, int newE) {
if (myVec->sz >= myVec->capacity) {
int newCap = myVec->capacity * 2;
myVec->data = realloc(myVec->data, newCap * sizeof(int));
} else {
for (int i = 0; i < myVec->capacity; i++) {
*(myVec->data + i) = *(myVec->data + i + 1);
}
myVec->data = &newE;
}
myVec->sz++;
}
void intVecPop(IntVec myVec) {
for (int i = 0; i < myVec->capacity; i++) {
*(myVec->data - i) = *(myVec->data - i + 1);
}
myVec->sz--;
}
This is the test file:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "intVec.c"
int main() {
struct IntVec v;
v.intVecPush(v,0);
return 0;
}
Every time I run the test file, I get the error:
test.c:7:16: error: variable has incomplete type 'struct IntVec'
struct IntVec v;
^
test.c:7:9: note: forward declaration of 'struct IntVec'
struct IntVec v;
^
1 error generated.
I've tried changing the #include "intVec.c" to "intVec.h" in the test file, however that produces the same error. What would I need to change in order to not get this error?
There is no structure definition struct IntVec.
So the compiler is unable to define the object v
struct IntVec v;
I think you mean
IntVec v;
And this call
v.intVecPush(v,0);
is invalid and does not make sense. I think there should be something like
IntVec v = intMakeEmptyVec();
intVecPush(v,0);
instead of
struct IntVec v;
v.intVecPush(v,0);
Also it is a bad idea to include the whole module in another module. You should place the structure definition in the header and include this header in the compilation unit with main.
That is move these definitions
typedef struct IntVecNode {
int* data;
int sz; // Number of elements that contain data
int capacity; // How much is allocated to the array
} IntVecNode;
typedef struct IntVecNode* IntVec;
in the header.
I'm trying to use a queue in my program, but it won't compile and I don't know why. The relevant part of the code is as follows.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#ifndef CUSTOMER
#define CUSTOMER
typedef int bool;
int r;
typedef struct{
int arrival;
int leaving;
} Customer;
static const int MAX_LENGTH = 100;
typedef struct{
int head;
int length;
Customer customer[MAX_LENGTH];
} CustomerLine;
void initializeQueue(CustomerLine* queue)
{
(*queue).head = 0;
(*queue).length = 0;
}
bool hasNext(CustomerLine* queue)
{
return (*queue).length > 0;
}
bool isFull(CustomerLine* queue)
{
return (*queue).length == MAX_LENGTH;
}
bool enqueue(CustomerLine* queue, Customer* customer)
{
if(isFull(queue))
return 0;
int index = ((*queue).head + (*queue).length) % MAX_LENGTH;
(*queue).customer[index] = *customer;
(*queue).length++;
return 1;
}
Customer* dequeue(CustomerLine* queue)
{
if(!hasNext(queue))
return 0;
Customer* result = &(*queue).customer[(*queue).head];
(*queue).length--;
(*queue).head = ((*queue).head + 1) % MAX_LENGTH;
return result;
}
The error says "Variably Modified 'customer' at file scope" I am a beginner at programming and just doing this is starting to get beyond my abilities so any help would be very much appreciated.
The line
static const int MAX_LENGTH = 100
is the problem. Replace it with
#define MAX_LENGTH 100
See why here and more explanations here or here or again here.
Furthermore:
You need an #endif after the #ifndef.
You need a main function somewhere.
In C, const means read-only, not constant and usable just like a macro. You cannot use a variable to specify the dimension of an array as you do here:
static const int MAX_LENGTH = 100;
typedef struct{
int head;
int length;
Customer customer[MAX_LENGTH]; /* Wrong. MAX_LENGTH is not a compile time constant. */
} CustomerLine;