I'm trying to compile this assignment I'm doing and I'm running into errors I'm not sure how to fix. Esepcially for Here's my command line argument:
gcc HW3.c semaphore.o buffer.c -L. -lst -o test
The problem is that the semaphor.h file was given to us so there isn't anything inherently wrong in the class so it shouldn't be complaining there I don't think. I'm not sure how to reconcile the struct errors either. I'm not particularly proficient in C. Here's the error list:
In file included from buffer.c:11:
semaphore.h:4: error: expected specifier-qualifier-list before ‘st_cond_t’
In file included from buffer.h:2,
from buffer.c:12:
semaphore.h:4: error: expected specifier-qualifier-list before ‘st_cond_t’
semaphore.h:5: error: conflicting types for ‘semaphore’
semaphore.h:5: note: previous declaration of ‘semaphore’ was here
semaphore.h:7: error: conflicting types for ‘down’
semaphore.h:7: note: previous declaration of ‘down’ was here
semaphore.h:8: error: conflicting types for ‘up’
semaphore.h:8: note: previous declaration of ‘up’ was here
semaphore.h:9: error: conflicting types for ‘createSem’
semaphore.h:9: note: previous declaration of ‘createSem’ was here
buffer.c: In function ‘init_buffer’:
buffer.c:20: error: incompatible type for argument 1 of ‘createSem’
semaphore.h:9: note: expected ‘struct semaphore *’ but argument is of type ‘semaphore’
buffer.c:21: error: incompatible types when assigning to type ‘semaphore’ from type ‘void *’
buffer.c:23: error: incompatible type for argument 1 of ‘createSem’
semaphore.h:9: note: expected ‘struct semaphore *’ but argument is of type ‘semaphore’
buffer.c:24: error: incompatible types when assigning to type ‘semaphore’ from type ‘void *’
buffer.c: In function ‘c_deposit’:
buffer.c:38: error: incompatible type for argument 1 of ‘down’
semaphore.h:7: note: expected ‘struct semaphore *’ but argument is of type ‘semaphore’
buffer.c:41: error: incompatible type for argument 1 of ‘up’
semaphore.h:8: note: expected ‘struct semaphore *’ but argument is of type ‘semaphore’
buffer.c: In function ‘c_remove’:
buffer.c:46: error: incompatible type for argument 1 of ‘down’
semaphore.h:7: note: expected ‘struct semaphore *’ but argument is of type ‘semaphore’
buffer.c:49: error: incompatible type for argument 1 of ‘up’
semaphore.h:8: note: expected ‘struct semaphore *’ but argument is of type ‘semaphore’
buffer.c:
#include <stdio.h>
#include <stdlib.h>
#include "semaphore.h"
#include "buffer.h"
buffer *init_buffer(int size)
{
buffer *new_Buffer;
new_Buffer=malloc((sizeof(buffer)));
createSem(new_Buffer->emptyBuffer, size);
new_Buffer->emptyBuffer=malloc(sizeof(semaphore));
createSem(new_Buffer->fullBuffer, 0);
new_Buffer->fullBuffer=malloc(sizeof(semaphore));
new_Buffer->chars=malloc(sizeof(char)*size);
new_Buffer->size=size;
new_Buffer->nextIn=0;
new_Buffer->nextOut=0;
return new_Buffer;
}
void c_deposit(buffer *buffer, char c)
{
down(buffer->emptyBuffer);
buffer->chars[buffer->nextIn]=c;
buffer->nextIn=(buffer->nextIn+1)%buffer->size;
up(buffer->fullBuffer);
}
int c_remove(buffer *buffer)
{
int c;
down(buffer->fullBuffer);
c=buffer->chars[buffer->nextOut];
buffer->nextOut=(buffer->nextOut+1)%buffer->size;
up(buffer->emptyBuffer);
return c;
}
buffer.h:
#include <stdio.h>
#include "semaphore.h"
typedef struct{
semaphore emptyBuffer;
semaphore fullBuffer;
int nextIn;
int nextOut;
int size;
char *chars;
}buffer;
void c_deposit(buffer *buffer, char c);
int c_remove(buffer *buffer);
buffer *init_buffer(int size);
For good measure here's semaphore.h as well:
typedef struct
{
int value;
st_cond_t sem_queue;
} semaphore;
void down(semaphore *s);
void up(semaphore *s);
void createSem(semaphore *s, int value);
Seems like semaphore.h needs to do this:
#include <st.h>
Also, you are including semaphore.h twice which is confusing the compiler.
Try putting this in semaphore.h as well:
#pragma once
See: http://en.wikipedia.org/wiki/Pragma_once
Related
When I compiled it following the TLDK installation instructions, an error occurred. The following is error.
In file included from /root/tldk/lib/libtle_l4p/ctx.c:23:
/root/tldk/lib/libtle_l4p/stream.h: In function ‘stream_get_dest’:
/root/tldk/lib/libtle_l4p/stream.h:156:6: error: dereferencing pointer to incomplete type ‘struct rte_ipv4_hdr’
l3h->src_addr = dev->prm.local_addr4.s_addr;
^~
/root/tldk/lib/libtle_l4p/stream.h:161:17: error: dereferencing pointer to incomplete type ‘struct rte_ipv6_hdr’
rte_memcpy(l3h->src_addr, &dev->prm.local_addr6,
^~
In file included from /root/tldk/lib/libtle_l4p/ctx.c:24:
/root/tldk/lib/libtle_l4p/misc.h: At top level:
/root/tldk/lib/libtle_l4p/misc.h:210:32: warning: ‘struct rte_ipv4_hdr’ declared inside parameter list will not be visible outside of this definition or declaration
_ipv4x_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, size_t ipv4h_len,
^~~~~~~~~~~~
/root/tldk/lib/libtle_l4p/misc.h: In function ‘_ipv4x_phdr_cksum’:
/root/tldk/lib/libtle_l4p/misc.h:215:15: error: dereferencing pointer to incomplete type ‘const struct rte_ipv4_hdr’
s0 = ipv4_hdr->src_addr;
^~
/root/tldk/lib/libtle_l4p/misc.h: At top level:
/root/tldk/lib/libtle_l4p/misc.h:246:15: warning: ‘struct rte_ipv4_hdr’ declared inside parameter list will not be visible outside of this definition or declaration
const struct rte_ipv4_hdr *ipv4_hdr)
^~~~~~~~~~~~
/root/tldk/lib/libtle_l4p/misc.h: In function ‘_ipv4_udptcp_mbuf_cksum’:
/root/tldk/lib/libtle_l4p/misc.h:250:28: warning: passing argument 1 of ‘_ipv4x_phdr_cksum’ from incompatible pointer type [-Wincompatible-pointer-types]
cksum = _ipv4x_phdr_cksum(ipv4_hdr, mb->l3_len, 0);
^~~~~~~~
/root/tldk/lib/libtle_l4p/misc.h:210:46: note: expected ‘const struct rte_ipv4_hdr *’ but argument is of type ‘const struct rte_ipv4_hdr *’
_ipv4x_phdr_cksum(const struct rte_ipv4_hdr *ipv4_hdr, size_t ipv4h_len,
~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
/root/tldk/lib/libtle_l4p/misc.h: At top level:
/root/tldk/lib/libtle_l4p/misc.h:270:15: warning: ‘struct rte_ipv6_hdr’ declared inside parameter list will not be visible outside of this definition or declaration
const struct rte_ipv6_hdr *ipv6_hdr)
^~~~~~~~~~~~
/root/tldk/lib/libtle_l4p/misc.h: In function ‘_ipv6_udptcp_mbuf_cksum’:
/root/tldk/lib/libtle_l4p/misc.h:274:30: warning: passing argument 1 of ‘rte_ipv6_phdr_cksum’ from incompatible pointer type [-Wincompatible-pointer-types]
cksum = rte_ipv6_phdr_cksum(ipv6_hdr, 0);
^~~~~~~~
In file included from /root/tldk/lib/libtle_l4p/ctx.c:21:
/root/dpdk/x86_64-native-linuxapp-gcc/include/rte_ip.h:390:44: note: expected ‘const struct ipv6_hdr *’ but argument is of type ‘const struct rte_ipv6_hdr *’
rte_ipv6_phdr_cksum(const struct ipv6_hdr *ipv6_hdr, uint64_t ol_flags)
~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
In file included from /root/tldk/lib/libtle_l4p/ctx.c:24:
/root/tldk/lib/libtle_l4p/misc.h: In function ‘check_pkt_csum’:
/root/tldk/lib/libtle_l4p/misc.h:322:44: error: dereferencing pointer to incomplete type ‘const struct rte_ipv4_hdr’
if (fl3 == PKT_RX_IP_CKSUM_UNKNOWN && l3h4->hdr_checksum != 0) {
^~
/root/tldk/lib/libtle_l4p/misc.h:336:39: error: dereferencing pointer to incomplete type ‘const struct rte_udp_hdr’
csum = (proto == IPPROTO_UDP && l4h->dgram_cksum == 0) ?
^~
/root/tldk/lib/libtle_l4p/misc.h:338:28: warning: passing argument 3 of ‘_ipv4_udptcp_mbuf_cksum’ from incompatible pointer type [-Wincompatible-pointer-types]
m->l2_len + m->l3_len, l3h4);
^~~~
/root/tldk/lib/libtle_l4p/misc.h:246:29: note: expected ‘const struct rte_ipv4_hdr *’ but argument is of type ‘const struct rte_ipv4_hdr *’
const struct rte_ipv4_hdr *ipv4_hdr)
~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
/root/tldk/lib/libtle_l4p/misc.h:341:28: warning: passing argument 3 of ‘_ipv6_udptcp_mbuf_cksum’ from incompatible pointer type [-Wincompatible-pointer-types]
m->l2_len + m->l3_len, l3h6);
^~~~
/root/tldk/lib/libtle_l4p/misc.h:270:29: note: expected ‘const struct rte_ipv6_hdr *’ but argument is of type ‘const struct rte_ipv6_hdr *’
const struct rte_ipv6_hdr *ipv6_hdr)
~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
/root/tldk/lib/libtle_l4p/ctx.c: At top level:
cc1: warning: unrecognized command line option ‘-Wno-address-of-packed-member’
make[3]: *** [/root/dpdk/mk/internal/rte.compile-pre.mk:116: ctx.o] Error 1
make[2]: *** [/root/tldk/mk/tle.lib.mk:27: all] Error 2
make[1]: *** [/root/tldk/mk/tle.subdir.mk:23: libtle_l4p] Error 2
make: *** [Makefile:44: lib] Error 2
Environment:
system version: debian 10.2
linux kernel version: 4.19.249-2
gcc version: 8.3.0
make version: 4.2.1
dpdk version: 18.11.11
Environment variables have been configured.
I'm try to use my struct peer_struct.h in multiple files. It is declared in main.c and I pass the struct trough reference.
The compiler gives me warings for the functions and I really don't understand why.
compiler Warnings:
In file included from first_use.c:1:0:
second_use.h:1:51: warning: ‘struct Peer_Information’ declared inside parameter list will not be visible outside of this definition or declaration
void second_use(char *message, int number, struct Peer_Information *peer);
^~~~~~~~~~~~~~~~
first_use.c: In function ‘first_use’:
first_use.c:6:23: warning: passing argument 3 of ‘second_use’ from incompatible pointer type [-Wincompatible-pointer-types]
second_use(me, 5, peer);
^~~~
In file included from first_use.c:1:0:
second_use.h:1:6: note: expected ‘struct Peer_Information *’ but argument is of type ‘struct Peer_Information *’
void second_use(char *message, int number, struct Peer_Information *peer);
^~~~~~~~~~
In file included from main.c:4:0:
first_use.h:1:23: warning: ‘struct Peer_Information’ declared inside parameter list will not be visible outside of this definition or declaration
void first_use(struct Peer_Information *peer);
^~~~~~~~~~~~~~~~
main.c: In function ‘main’:
main.c:28:15: warning: passing argument 1 of ‘first_use’ from incompatible pointer type [-Wincompatible-pointer-types]
first_use(&peer);
^
In file included from main.c:4:0:
first_use.h:1:6: note: expected ‘struct Peer_Information *’ but argument is of type ‘struct Peer_Information *’
void first_use(struct Peer_Information *peer);
peer_struct.h
struct Peer_Information {
char ownIP[16];
char ownPort[6];
unsigned int ownID;
char successor_IP[16];
char successor_Port[6];
unsigned int successor_ID;
char predecessor_IP[16];
char predecessor_Port[6];
unsigned int predecessor_ID;
} Peer_Information;
main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "first_use.h"
#include "peer_struct.h"
void init_peer(char *argv[], struct Peer_Information *peer){
//Fills struct up, not import for question
}
int main(int argc, char *argv[]) {
if (argc != 10) {
printf("Nicht genügend Parameter \n");
return -1;
}
struct Peer_Information peer;
init_peer(argv, &peer);
first_use(&peer);
return 0;
}
first_use.c
#include "second_use.h"
#include "peer_struct.h"
void first_use(struct Peer_Information *peer) {
char me[] = "Hello";
second_use(me, 5, peer);
}
first_use.h
void first_use(struct Peer_Information *peer);
second_use.c
#include <stdio.h>
#include <stdlib.h>
#include "peer_struct.h"
void second_use(char *message, int number, struct Peer_Information *peer) {
printf("%d", peer->ownID);
}
second_use.h
void second_use(char *message, int number, struct Peer_Information *peer);
The very first warning tells it all. When you include first_use.h in main.c, the only thing which gets included is the prototype for the function
void first_use(struct Peer_Information *peer);
The type Peer_Information is not yet defined, so compiler generates a type with the same name which is local to this function. Than nothing works as it should, because you end up calling the function with the different (from compiler's standpoint) type which just happen to have the same name.
To fix it, you should make it a habit to include all header files required for the header files. In your case, include definition of the type as well and make your first_use.h to be like following:
#include "peer_struct.h"
void first_use(struct Peer_Information *peer);
In addition to that, your include file misses so-called include guard. You can get more details about those here: What exactly do C include guards do?
In my project I have these 2 files. When I try to compile it a get some peculiar warnings that I cannot solve, though my program runs fine. I use the -Wall and -Wextra arguments at compile.
//File: DataStructures.h
typedef struct EntryListPacketStruct EntryListPacket;
typedef struct IndexesPacketStruct IndexesPacket;
typedef struct MatchingQueriesResultStruct MatchingQueriesResult;
typedef struct DataPacketStruct* DataPacketPtr;
extern DataPacketPtr Packet;
EntryListPacket* allocateEntryListPacket(void);
void initializeDataPacket(DataPacketPtr);
void freeDataPacket(DataPacketPtr);
void initializeEntryListPacket(EntryListPacket*);
void freeEntryListsPacket(EntryListPacket*);
I include the DataStructures.h in the following file:
//File: DataStructures.c
struct EntryListPacketStruct {
EntryList* exactTypeEntryList;
EntryList** editDistEntryLists;
EntryList** hammingDistEntryLists;
};
struct DataPacketStruct {
struct EntryListPacket* entryLists;
struct IndexesPacket* indexes;
dllistptr matchingQResultList;
};
EntryListPacket* allocateEntryListPacket(void){
EntryListPacket* temp=malloc(sizeof(EntryListPacket));
return temp;
}
void initializeDataPacket(DataPacketPtr packet){
DataPacketPtr temp;
temp=malloc(sizeof(struct DataPacketStruct));
initializeEntryListPacket(temp->entryLists);
initializeIndexesPacket(temp->indexes);
packet=temp;
}
void freeDataPacket(DataPacketPtr packet){
freeEntryListsPacket(packet->entryLists);
freeIndexesPacket(packet->indexes);
free(packet);
}
void initializeEntryListPacket(EntryListPacket* packet) {
packet->exactTypeEntryList = NULL;
packet->editDistEntryLists = malloc(sizeof (EntryList)*22);
int i;
for (i = 0; i < 22; i++) {
packet->editDistEntryLists[i] = NULL;
}
packet->hammingDistEntryLists = malloc(sizeof (EntryList)*27);
for (i = 0; i < 27; i++) {
packet->hammingDistEntryLists[i] = NULL;
}
}
void freeEntryListsPacket(EntryListPacket* packet){
if(packet->exactTypeEntryList!=NULL)
DestroyEntryList(packet->exactTypeEntryList);
free(packet->exactTypeEntryList);
int i;
for(i=0;i<22;i++){
if(packet->editDistEntryLists[i]!=NULL){
printf("%d\n",i);
DestroyEntryList(packet->editDistEntryLists[i]);
}
free(packet->editDistEntryLists[i]);
}
free(packet->editDistEntryLists);
for(i=0;i<27;i++){
if(packet->hammingDistEntryLists[i]!=NULL){
printf("%d\n",i);
DestroyEntryList(packet->hammingDistEntryLists[i]);
}
free(packet->hammingDistEntryLists[i]);
}
free(packet->hammingDistEntryLists);
free(packet);
}
//File:main.c
EntryListPacket *temp;
temp=allocateEntryListPacket();
initializeEntryListPacket(temp);
freeEntryListsPacket(temp);
And I get the following Warnings:
DataStructures.c: In function ‘initializeDataPacket’:
DataStructures.c:48:6: warning: passing argument 1 of ‘initializeEntryListPacket’ from incompatible pointer type [enabled by default]
initializeEntryListPacket(temp->entryLists);
^
In file included from DataStructures.c:6:0:
DataStructures.h:27:10: note: expected ‘struct EntryListPacket *’ but argument is of type ‘struct EntryListPacket *’
void initializeEntryListPacket(EntryListPacket*);
^
DataStructures.c:50:6: warning: passing argument 1 of ‘initializeIndexesPacket’ from incompatible pointer type [enabled by default]
initializeIndexesPacket(temp->indexes);
^
In file included from DataStructures.c:6:0:
DataStructures.h:30:10: note: expected ‘struct IndexesPacket *’ but argument is of type ‘struct IndexesPacket *’
void initializeIndexesPacket(IndexesPacket*);
^
DataStructures.c:43:42: warning: parameter ‘packet’ set but not used [-Wunused-but-set-parameter]
void initializeDataPacket(DataPacketPtr packet){
^
DataStructures.c: In function ‘freeDataPacket’:
DataStructures.c:57:6: warning: passing argument 1 of ‘freeEntryListsPacket’ from incompatible pointer type [enabled by default]
freeEntryListsPacket(packet->entryLists);
^
In file included from DataStructures.c:6:0:
DataStructures.h:28:10: note: expected ‘struct EntryListPacket *’ but argument is of type ‘struct EntryListPacket *’
void freeEntryListsPacket(EntryListPacket*);
^
DataStructures.c:58:6: warning: passing argument 1 of ‘freeIndexesPacket’ from incompatible pointer type [enabled by default]
freeIndexesPacket(packet->indexes);
^
In file included from DataStructures.c:6:0:
DataStructures.h:31:10: note: expected ‘struct IndexesPacket *’ but argument is of type ‘struct IndexesPacket *’
void freeIndexesPacket(IndexesPacket*);
I cannot figure out what I am doing wrong or if the compiler is stuck since this for example: DataStructures.h:28:10: note: expected ‘struct EntryListPacket *’ but argument is of type ‘struct EntryListPacket *’
doesn't seem reasonable to me.
Thx in advance!
Your struct tags include a Struct suffix, but you omitted it when you declared:
struct DataPacketStruct {
struct EntryListPacket* entryLists;
struct IndexesPacket* indexes;
dllistptr matchingQResultList;
};
It should be:
struct DataPacketStruct {
struct EntryListPacketStruct* entryLists;
struct IndexesPacketStruct* indexes;
dllistptr matchingQResultList;
};
Or just:
struct DataPacketStruct {
EntryListPacket* entryLists;
IndexesPacket* indexes;
dllistptr matchingQResultList;
};
Also, your initializeDataPacket function is leaking the memory. Assigning temp to packet doesn't return the value since packet is passed by value, not by reference.
In DataStructures.h, try replacing this:
struct DataPacketStruct {
struct EntryListPacket* entryLists;
...
With this:
struct DataPacketStruct {
EntryListPacket* entryLists;
...
Or this:
struct DataPacketStruct {
struct EntryListPacketStruct* entryLists;
...
And similarly for struct IndexesPacket* indexes;. The compiler warning comes from missing the typedef symbol up with the struct symbol itself.
I have the following code to print the directory listing in unix.
struct dirent *res;
struct DIR *dir;
scanf("%s",str);
dir=opendir(str);
if(dir==NULL)
{
perror("Invalid directory");
return 1;
}
res=(struct dirent *)readdir(dir);
while(res)
{
printf("%s\n",res->d_name);
res=(struct dirent *)readdir(dir);
}
When I compile the above code I get the following warning
ls.c:16:17: warning: passing argument 1 of ‘readdir’ from incompatible pointer type
[enabled by default]
/usr/include/dirent.h:164:23: note: expected ‘struct DIR *’ but argument is of type
‘struct DIR *’
ls.c:20:21: warning: passing argument 1 of ‘readdir’ from incompatible pointer type
[enabled by default]
/usr/include/dirent.h:164:23: note: expected ‘struct DIR *’ but argument is of type
‘struct DIR *’
What does GCC exactly mean when it says "Expected argument foo but argument is of type foo"?
I also tried using struct DIR dir instead of *dir and &dir instead of dir,but it results in following error
ls.c:7:12: error: storage size of ‘dir’ isn’t known
PS:The output of the code is perfectly OK.
DIR is a macro which generally expands to struct something, so you're declaring struct struct something *dir. Which is apparently a confusing thing to do (though, also apparently, fine by GCC), leading to a confusing error message. The solution is simply to declare DIR *dir, without the struct.
Ben has the correct solution to your problem, but this does look like a serious problem in how gcc reports this error.
First of all, it wasn't a macro issue. DIR is a typedef for struct __DIR (at least that's what it is here, and I get the same error message). There is no struct DIR except the one declared by struct DIR *dir; but gcc seems to be saying there is another type with that name.
This sample compilation unit demonstrates the problem more clearly:
struct foo {
int a,b,c;
};
typedef struct foo bar;
void do_bar(bar *);
void func(void)
{
int i = 0;
/* do_bar wants a bar *, which is a struct foo *, but we're giving it an
int * instead. What will gcc say? */
do_bar(&i);
}
gcc reports:
t.c: In function ‘func’:
t.c:15:7: warning: passing argument 1 of ‘do_bar’ from incompatible pointer type [enabled by default]
t.c:7:10: note: expected ‘struct bar *’ but argument is of type ‘int *’
but there is no struct bar in the code at all. It has taken the bar typedef and crammed the word struct in front of it for no reason.
I have a program written to evaluate postfix expressions. I have the code fully functioning with no compiler warnings when I compile and run it from a Windows IDE (Codeblocks), however, when I try to compile the source code in a Linux environment, I get a ton or warnings. They are listed below:
postfix.c: In function ‘infixToPostfix’:
postfix.c:20: warning: passing argument 1 of ‘stackInit’ from incompatible pointer type
stack.h:25: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:36: warning: passing argument 1 of ‘stackPush’ from incompatible pointer type
stack.h:31: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:40: warning: passing argument 1 of ‘stackIsEmpty’ from incompatible pointer type
stack.h:37: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:42: warning: passing argument 1 of ‘stackPeek’ from incompatible pointer type
stack.h:43: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:44: warning: passing argument 1 of ‘stackPeek’ from incompatible pointer type
stack.h:43: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:45: warning: passing argument 1 of ‘stackPop’ from incompatible pointer type
stack.h:34: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:49: warning: passing argument 1 of ‘stackPush’ from incompatible pointer type
stack.h:31: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:54: warning: passing argument 1 of ‘stackPeek’ from incompatible pointer type
stack.h:43: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:56: warning: passing argument 1 of ‘stackPop’ from incompatible pointer type
stack.h:34: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:59: warning: passing argument 1 of ‘stackPop’ from incompatible pointer type
stack.h:34: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:63: warning: passing argument 1 of ‘stackIsEmpty’ from incompatible pointer type
stack.h:37: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:65: warning: passing argument 1 of ‘stackPop’ from incompatible pointer type
stack.h:34: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:69: warning: passing argument 1 of ‘stackDestroy’ from incompatible pointer type
stack.h:28: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c: In function ‘evaluatePostfix’:
postfix.c:139: warning: passing argument 1 of ‘stackInit’ from incompatible pointer type
stack.h:25: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:146: warning: passing argument 1 of ‘stackPush’ from incompatible pointer type
stack.h:31: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:150: warning: passing argument 1 of ‘stackPop’ from incompatible pointer type
stack.h:34: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:151: warning: passing argument 1 of ‘stackPop’ from incompatible pointer type
stack.h:34: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:154: warning: passing argument 1 of ‘stackPush’ from incompatible pointer type
stack.h:31: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:159: warning: passing argument 1 of ‘stackPop’ from incompatible pointer type
stack.h:34: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
postfix.c:160: warning: passing argument 1 of ‘stackDestroy’ from incompatible pointer type
stack.h:28: note: expected ‘struct stack *’ but argument is of type ‘struct stack **’
/tmp/ccPMgl0G.o: In function `applyOperator':
postfix.c:(.text+0x6bd): undefined reference to `pow'
collect2: ld returned 1 exit status
They all seem to be related to my postfix.c source and my stack.h header. The postfix.c source I completely modified myself, but the stack.h header was supplied by my instructor. All the errors for the postfix.c source seem to point to lines where I have code in the following manner:
stackInit(&s);
I believe it is referring to my use of the ampersand as a parameter for the function... but there isn't any other way for me to indicate that I am modifying the immediate value of 's' is there? Is there something I should be including before hand? Also... for the 'pow' issue, I have included the header file:
math.h
So it should be able to reference it... I don't know why it won't compile :/ I have been using this to compile my 3 source files together:
gcc prog2.c stack.c postfix.c
Is there another way I should be doing this? Thank you in advance.
Source Code:
/* function to convert an infix to postfix */
char *infixToPostfix(char *infixStr)
{
static char pfline[30];
int i;
stack * s;
stackInit(&s);
char * token = strtok(infixStr, " ");
for(i = 0; i < 30; ++i) {
pfline[i] = '\0';
}
while(token != NULL)
{
if(isOperand(token) != 0) {
strcat(pfline, token);
strcat(pfline, " ");
}
if(isLeftParen(token))
stackPush(&s, token);
if(isOperator(token))
{
if(!stackIsEmpty(&s))
{
if(isOperator(stackPeek(&s)))
{
if(stackPrecedence(stackPeek(&s)) >= inputPrecedence(token))
strcat(pfline, stackPop(&s));
strcat(pfline, " ");
}
}
stackPush(&s, token);
}
if(isRightParen(token))
{
while(!isLeftParen(stackPeek(&s)))
{
strcat(pfline, stackPop(&s));
strcat(pfline, " ");
}
stackPop(&s);
}
token = strtok(NULL, " ");
}
while(!stackIsEmpty(&s))
{
strcat(pfline, stackPop(&s));
strcat(pfline, " ");
}
printf("%s\n", pfline);
stackDestroy(&s);
return pfline;
}
int evaluatePostfix(char *postfixStr)
{
stack * s;
int x = 0, y = 0, z = 0;
stackInit(&s);
char * token = strtok(postfixStr, " ");
while(token != NULL)
{
if(isOperand(token) != 0)
stackPush(&s, token);
if(isOperator(token))
{
y = atoi(stackPop(&s));
x = atoi(stackPop(&s));
char *str = malloc(10 * sizeof(char));
sprintf(str, "%d", applyOperator(x, y, token));
stackPush(&s, str);
}
token = strtok(NULL, " ");
}
z = atoi(stackPop(&s));
stackDestroy(&s);
return z;
}
Here is my stack.h header file that was provided which is an interface for the stack:
/*
* This is an interface for a stack of strings.
*
*/
#ifndef _STACK_H
#define _STACK_H
#include <stdbool.h>
typedef char * stkElement;
struct stkNode {
stkElement element;
struct stkNode *next;
};
typedef struct stkNode stkNode;
typedef struct {
stkNode *top;
} stack;
/* function to initialize a new stack variable */
void stackInit(stack *stkPtr);
/* function to free the memory associated with the stack */
void stackDestroy(stack *stkPtr);
/* function to add an element to the top of the stack */
void stackPush(stack *stkPtr, stkElement element);
/* function that removes the element from the top of the stack */
stkElement stackPop(stack *stkPtr);
/* function that returns a true value if the stack is empty */
bool stackIsEmpty(stack *stkPtr);
/* function that returns the number of elements in the stack */
int stackLength(stack *stkPtr);
/* function that returns the top element in the stack without removing it */
stkElement stackPeek(stack *stkPtr);
#endif /* _STACK_H */
It is likely not the call to stackInit() that is actually wrong - rather it is the declaration of s. You are correct that you need to use stackInit(&s); if you want stackInit() to modify the variable s in the calling function, but if stackInit() takes a parameter of type stack * then that indicates that you should be declaring s as:
stack s;
You didn't post the relevant code, but in order for the error message to make sense, I have to assume that s is a pointer to a stack and stackInit takes a pointer to a stack.
In that case, you should call it as stackInit(s) and calling it as stackInit(&s) is simply wrong. If you really want stackInit to accept a pointer to a pointer to a stack, you should change the declaration to say so (however I can't imagine that that would make sense).
That being said the only actual compile errror here is "undefined reference to 'pow'", which is unrelated to the warnings. That error can be fixed by linking against the math library using the -lm flag to gcc.