I'm working on a Priority queue with an array implementation. Everything seems to work fine but I get this error: conflicting types for 'remove'
I've declared the function in its header file, I've included the header file but the compiler stil complains. I think the problem resides somewhere else.
Here is pqueue.h:
#ifndef PQUEUE_H
#define PQUEUE_H
//---------------
#define HIGHP 0
#define MEDP 1
#define LOWP 2
#define MAXEL 10
#include <stddef.h>
typedef struct message {
char data[100];
int priority;
} message;
typedef struct pQueue {
struct message messages[10];
int rear;
int front;
int size;
} pQueue;
void initPQueue(pQueue *pq);
void add(pQueue *pq, char *data, int pri);
char* remove(struct pQueue *pq); // Error: conflicting types for: 'remove'
int isEmpty(pQueue *pq);
#endif
pqueue.c:
#include "pqueue.h"
#include <string.h>
void initPQueue(pQueue *pq) {
pq->front = 0;
pq->rear = 0;
pq->size = 0;
}
void add(pQueue *pq, char *data, int pri) {
if (pq->size > MAXEL) {
return; // NOTE: data is lost
}
message m;
strcpy(m.data, data);
m.priority = pri;
if (isEmpty(pq)) {
pq->messages[pq->rear] = m;
pq->rear = (pq->rear % (MAXEL - 1)) + 1;
return; // done
}
/**TODO: NEEDS REPAIR**/
int i = 0;
int j = 0;
for (; i < pq->rear; i = (i % (MAXEL - 1)) + 1) {
if (m.priority > pq->messages[i].priority) {
// found element with higher or equal priority
for (j = pq->rear - 1; j >= i; j = (j % (MAXEL - 1)) - 1) {
pq->messages[j] = pq->messages[j - 1];
}
break;
}
}
pq->messages[i] = m;
/****/
pq->size++;
}
char* remove(struct pQueue *pq) {
if (isEmpty(pq)) {
return NULL ;
}
pq->size--;
return pq->messages[pq->front].data;
}
int isEmpty(pQueue *pq) {
if (!pq->size)
return 1;
return 0;
}
Any thoughts?
int remove(const char *path) is a standard function. You need to choose a different name for yours.
To echo what everyone else has said, there is a standard function. But to add to this, in C, you should always prefix your functions with the name of your library and of the type that it operates on to avoid these sorts of issues. For example, it would be better to name your function something like:
mylibrary_pqueue_remove
This would avoid naming clashes with the standard library and with other peoples' code.
remove is a reserved identifier, you can't use it in your program, as long as you include <stdio.h>.
7.21.4.1 The remove function
#include <stdio.h>
int remove(const char *filename);
The remove function causes the file whose name is the string pointed to by
filename to be no longer accessible by that name. A subsequent attempt
to open that file using that name will fail, unless it is created
anew. If the file is open, the behavior of the remove function is
implementation-defined.
Related
I'm implementing a data structure in C and I get this error in my test file. Without adding code because then that would be a huge post with a ton of code to go through, but here's what my code looks like:
header.h file:
typedef struct array Arr;
functions.c file:
#include "header.h"
struct array{
int number;
int size;
char *names;
}
main.c file:
#include "header.h"
bool function(const Arr *const variable)
{
for (int i = 0; i < variable->size; i++)
{
variable->number[i] = i;
}
}
and so I get the error mentioned in the title referring to Arr*->number and Arr->*size. What I suspect to be the issue is that Arr is only typedefed but not defined. If that's the case, how can I resolve it?
Here's the main code:
main.c
#include <stdio.h>
#include "header.h"
int main(){
set *setA = set_empty();
set_insert(69,setA );
set_insert(15, setA);
set *setB = set_empty();
set_insert(12,setB );
set_insert(15, setB);
set *setDiff = set_difference(setA, setB);
printf("\n");
print_set(setDiff);
bool diff = verify_difference(setDiff, setA, setB);
}
bool verify_difference(const set *const setDiff, const set *const setA, const struct set *const setB)
{
bool answer = true;
for (int x = 0; x < setDiff->size; x++)
{
if (set_member_of(setDiff->array[x], setA) && set_member_of(setDiff->array[x], setB))
{
answer = false;
break;
}
}
return answer;
}
header.h
#ifndef HEADER_H
#define HEADER_H
#include <stdbool.h>
typedef struct set set;
set *set_empty();
void set_insert(const int value, set *s);
bool set_member_of(const int value, const set *const s);
functions.c
#include <stdio.h>
#include "header.h"
struct set {
int capacity;
int size;
char *array;
};
set *set_empty()
{
struct set *ptr = malloc(sizeof(struct set));
ptr->size = 0;
ptr->array = malloc(sizeof(char));
ptr->capacity = 1;
return ptr;
}
void set_insert(const int value, set *s)
{
if (!set_member_of(value, s)) {
int bit_in_array = value; // To make the code easier to read
// Increase the capacity if necessary
if (bit_in_array >= s->capacity) {
int no_of_bytes = bit_in_array / 8 + 1;
s->array = realloc(s->array, no_of_bytes);
for (int i = s->capacity / 8 ; i < no_of_bytes ; i++) {
s->array[i] = 0;
}
s->capacity = no_of_bytes * 8;
}
// Set the bit
int byte_no = bit_in_array / 8;
int bit = 7 - bit_in_array % 8;
s->array[byte_no] = s->array[byte_no] | 1 << bit;
s->size++;
}
}
set *set_difference(const set *const s1, const set *const s2)
{
struct set *s = set_empty();
for (int i = 0; i < s1->size; i++)
{
if (!set_member_of(s1->array[i], s2))
{
set_insert(s1->array[i], s);
}
}
for (int i = 0; i < s2->size; i++)
{
if (!set_member_of(s2->array[i], s1))
{
set_insert(s2->array[i], s);
}
}
return s;
}
bool set_member_of(const int value, const set *const s)
{
int bit_in_array = value;
if (bit_in_array >= s->capacity) {
return false;
}
int byte_no = bit_in_array / 8;
int bit = 7 - bit_in_array % 8;
char the_byte = s->array[byte_no];
return the_byte & 1 << bit;
}
The definition of the structure shall be available in main. Otherwise the compiler does not know whether there is the data member number in the structure referred in this statement
Arr->number[i] = i;
Moreover in any case this statement is incorrect because Arr is a type specifier and according to the structure definition the data member number is not an array
It seems you mean
variable[i].number = i;
But as the function parameter
bool function(const Arr *const variable)
is declared as a pointer to a constant object then you may not change pointed to data members of the structure.
So either move the definition of the function function from main.c in functions.c or place the structure definition in the header file.
And there is a typo
Typedef struct array Arr;
^^T
you need to use lower case letter
typedef struct array Arr;
I can only hazard a guess. Your code snippet could be wrong.
Move the structure definition to header.h & check.
//header.h file:
typedef struct array Arr;
struct array{
int number;
int size;
char *names;
};
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 working on a hash table that stores strings in linked lists so I can avoid collisions. However, I'm getting two errors that I'm not sure how to fix. The first error I am getting is in the line that says NewT->Table[i] == NULL;. It's saying warning: statement with no effects [-Wunused-value].
The second error I'm getting is in the same function. The error is in the line return NewT and the error is warning: return from incompatible pointer type[enabled by default]. I've been staring at this for awhile and I can't see where there is an unused value and I have no idea what the return error means even after a bit of research. Can someone explain these to me and help me fix them?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define HASH_MULTIPLIER 65599
/*Structures*/
typedef struct List_T
{
char *str;
int count;
struct List_T *next;
} ListT;
typedef struct Hash_T
{
int htsize;
ListT **Table;
} HashT;
/*Prototypes*/
unsigned int hash(const char *str);
HashT **ht_create(void);
int htsize;
int main(int argc, char *argv[])
{
if (argc <= 1)
{
printf("Please declare a table size");
return 1;
}
htsize = atoi(argv[1]);
return 0;
}
unsigned int hash(const char *str)
{
int i;
unsigned int h = 0U;
for (i = 0; str[i] != '\0'; i++)
h = h * HASH_MULTIPLIER + (unsigned char) str[i];
return h % htsize;
}
HashT **ht_create(void)
{
HashT *NewT;
int i;
if (htsize < 1) //invalid size for
{
fprintf(stderr,"Invalid Size for table");
exit(0);
}
if ((NewT = malloc(sizeof(HashT))) == NULL)
{
fprintf(stderr,"Invalid size for table");
exit(0);
}
if ((NewT->Table = malloc(sizeof(ListT *) * htsize)) == NULL)
{
fprintf(stderr,"Invalid size for table");
exit(0);
}
for (i = 0; i<htsize; i++)
{
NewT->Table[i] == NULL;
}
NewT->htsize = htsize;
return NewT;
}
The first error I am getting is in the line that says NewT->Table[i]
== NULL;. It's saying warning: statement with no effects [-Wunused-value].
This error shows up because the code is making a comparison and not an assignment. The value returned by the comparison (is Table[i] null?) is itself not assigned to anything else, which means it's unused.
Keep a single = operator instead of the doubled == to make sure you're actually assigning instead of comparing.
The second error I'm getting is in the same function. The error is in
the line return NewT and the error is warning: return from
incompatible pointer type[enabled by default].
Your function claims to be returning a pointer to a pointer to HashT, or HashT **, but you end up returning a pointer to HashT, or HashT * instead, which is the type of your NewT variable.
Your function's signature should use a single * instead of two.
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;