help with pointers, files and functions in c - c

throw me 3 errors when compiling I am using Dev-C + +.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct agenda{
char nombre [50];
char telefono[25];
char email[50];
}
struct nodo{
struct agenda dato;
struct nodo *proximo;
}
struct nodo *nuevonodo
int colavacia(struct nodo *)
struct nodo *creacola (struct nodo *, struct agenda);
void mostrar (struct nodo *);
void main()
{
struct nodo *pri=NULL, *ult=NULL;
struct agenda x;
printf ("ingrese nombre: ");
gets(x.nombre);
while (strcmpi(x.nombre, "fin"))
{
printf ("ingrese telefono: ");
gets (x.telefono);
printf ("ingrese mail: ");
gets(x.mail);
ult=creacola(ult,x);
if (pri==NULL) pri=ult; //si es la 1 pasada pongo en pri el valor del primer nodo
printf ("ingrese nombre: ");
gets(x.nombre);
}
if (colavacia(pri)==1)
{
printf ("No se ingresaron registros");getch();
}
else mostrar(pri);
}
struct nodo *nuevonodo()
{
struct nodo *p;
p=(struct nodo *)malloc(sizeof(struc nodo));
if(p==NULL)
{
printf ("memoria ram llena");
getch();
exit(0);
}
return p;
}
struct nodo *creacola(struct nodo *ult, struct agenda x)
{
struct nodo *p;
p=nuevonodo();
(*P).dato=x;
(*p).proximo=NULL;
if(ult!=NULL) (*ult).proximo=p; //si hay nodo anterior en prox pongo la direccion del nodo actual
return p;
}
int colavacia(struct nodo *pri)
{
if(pri==NULL) return 1;
else
return 0;
}
void mostrar (struct nodo *pri)
{
struct nodo *aux;
while(pri!=NULL)
{
printf("Nombre: %s - telefono: %s - Mail: %s \n",
pri->dato.nombre,pri->dato.telefono,pri->dato.mail);
aux=pri;
pri=(*pri).proximo;
free(aux);
}
getch();
}

You must end a struct definition with a semicolon:
struct agenda{
char nombre [50];
char telefono[25];
char email[50];
}; // <- HERE
Similarly for variable and function declarations:
struct nodo *nuevonodo;
int colavacia(struct nodo *);
And the return value of main has type int.

5587527.c:2:19: fatal error: conio.h: No such file or directory
I've commented out the offending #include and tried again
5587527.c:11:5: error: two or more data types in declaration specifiers
5587527.c:16:5: error: two or more data types in declaration specifiers
5587527.c:17:12: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘int’
cc1: warnings being treated as errors
5587527.c:22:10: error: function declaration isn’t a prototype
5587527.c:22:10: error: return type of ‘main’ is not ‘int’
5587527.c: In function ‘main’:
5587527.c:22:10: error: old-style function definition
5587527.c:29:9: error: implicit declaration of function ‘strcmpi’
5587527.c:29:9: error: nested extern declaration of ‘strcmpi’
5587527.c:35:15: error: ‘struct agenda’ has no member named ‘mail’
5587527.c:36:9: error: implicit declaration of function ‘creacola’
5587527.c:36:9: error: nested extern declaration of ‘creacola’
5587527.c:36:12: error: assignment makes pointer from integer without a cast
5587527.c:41:5: error: implicit declaration of function ‘colavacia’
5587527.c:41:5: error: nested extern declaration of ‘colavacia’
5587527.c:43:5: error: implicit declaration of function ‘getch’
5587527.c:43:5: error: nested extern declaration of ‘getch’
5587527.c: At top level:
5587527.c:48:18: error: function declaration isn’t a prototype
5587527.c: In function ‘nuevonodo’:
5587527.c:48:18: error: old-style function definition
5587527.c:51:40: error: ‘struc’ undeclared (first use in this function)
5587527.c:51:40: note: each undeclared identifier is reported only once for each function it appears in
5587527.c:51:46: error: expected ‘)’ before ‘nodo’
5587527.c: At top level:
5587527.c:61:18: error: conflicting types for ‘creacola’
5587527.c:36:13: note: previous implicit declaration of ‘creacola’ was here
5587527.c: In function ‘creacola’:
5587527.c:65:11: error: ‘P’ undeclared (first use in this function)
5587527.c: In function ‘mostrar’:
5587527.c:85:54: error: ‘struct agenda’ has no member named ‘mail’
I've added a ; to lines 10, 14, 16, 17 and tried again
cc1: warnings being treated as errors
5587527.c:22:10: error: function declaration isn’t a prototype
5587527.c:22:10: error: return type of ‘main’ is not ‘int’
5587527.c: In function ‘main’:
5587527.c:22:10: error: old-style function definition
5587527.c:29:9: error: implicit declaration of function ‘strcmpi’
5587527.c:29:9: error: nested extern declaration of ‘strcmpi’
5587527.c:35:15: error: ‘struct agenda’ has no member named ‘mail’
5587527.c:43:5: error: implicit declaration of function ‘getch’
5587527.c:43:5: error: nested extern declaration of ‘getch’
5587527.c: At top level:
5587527.c:48:18: error: function declaration isn’t a prototype
5587527.c:48:18: error: ‘nuevonodo’ redeclared as different kind of symbol
5587527.c:16:18: note: previous declaration of ‘nuevonodo’ was here
5587527.c: In function ‘nuevonodo’:
5587527.c:48:18: error: old-style function definition
5587527.c:51:40: error: ‘struc’ undeclared (first use in this function)
5587527.c:51:40: note: each undeclared identifier is reported only once for each function it appears in
5587527.c:51:46: error: expected ‘)’ before ‘nodo’
5587527.c: In function ‘creacola’:
5587527.c:65:11: error: ‘P’ undeclared (first use in this function)
5587527.c: In function ‘mostrar’:
5587527.c:85:54: error: ‘struct agenda’ has no member named ‘mail’
Fix main in line 22 (to int main(void))
5587527.c: In function ‘main’:
5587527.c:29:9: error: implicit declaration of function ‘strcmpi’
5587527.c:29:9: error: nested extern declaration of ‘strcmpi’
5587527.c:35:15: error: ‘struct agenda’ has no member named ‘mail’
5587527.c:43:5: error: implicit declaration of function ‘getch’
5587527.c:43:5: error: nested extern declaration of ‘getch’
5587527.c: At top level:
5587527.c:48:18: error: function declaration isn’t a prototype
5587527.c:48:18: error: ‘nuevonodo’ redeclared as different kind of symbol
5587527.c:16:18: note: previous declaration of ‘nuevonodo’ was here
5587527.c: In function ‘nuevonodo’:
5587527.c:48:18: error: old-style function definition
5587527.c:51:40: error: ‘struc’ undeclared (first use in this function)
5587527.c:51:40: note: each undeclared identifier is reported only once for each function it appears in
5587527.c:51:46: error: expected ‘)’ before ‘nodo’
5587527.c: In function ‘creacola’:
5587527.c:65:11: error: ‘P’ undeclared (first use in this function)
5587527.c: In function ‘mostrar’:
5587527.c:85:54: error: ‘struct agenda’ has no member named ‘mail’
Changed strcmpi in line 29 to strcmp
5587527.c: In function ‘main’:
5587527.c:35:15: error: ‘struct agenda’ has no member named ‘mail’
cc1: warnings being treated as errors
5587527.c:43:5: error: implicit declaration of function ‘getch’
5587527.c:43:5: error: nested extern declaration of ‘getch’
5587527.c: At top level:
5587527.c:48:18: error: function declaration isn’t a prototype
5587527.c:48:18: error: ‘nuevonodo’ redeclared as different kind of symbol
5587527.c:16:18: note: previous declaration of ‘nuevonodo’ was here
5587527.c: In function ‘nuevonodo’:
5587527.c:48:18: error: old-style function definition
5587527.c:51:40: error: ‘struc’ undeclared (first use in this function)
5587527.c:51:40: note: each undeclared identifier is reported only once for each function it appears in
5587527.c:51:46: error: expected ‘)’ before ‘nodo’
5587527.c: In function ‘creacola’:
5587527.c:65:11: error: ‘P’ undeclared (first use in this function)
5587527.c: In function ‘mostrar’:
5587527.c:85:54: error: ‘struct agenda’ has no member named ‘mail’
...
I've done a few correct errors / recompile / get more errors for you. Now it's your turn.

Related

When I am compiling tldk an error occured: dereferencing pointer to incomplete type . Does this mean there is a problem with the source code?

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.

Multitude of struct errors

It's an extremely simple and useless piece of practice code I'm working with in what is starting to seem like and extremely useless book. I was doing a struct exercise, and upon code compilation I received a handful of errors.
Here's the offending code:
struct fish = {
const char *name;
const char *species;
int teeth;
int age;
};
void catalog(struct fish f)
{
printf("%s is a %s with %i teeth. He is %i.\n", f.name, f.species, f.teeth, f.age);
}
int main()
{
struct fish snappy = {"Snappy", "piranha", 69, 4};
catalog(snappy);
return 0;
}
This is the exact code from the book, minus the struct definition above catalog. I ended up just copy pasting because I started to suspect this book was just dead wrong. The book claimed that the above code should compile and run without the struct even being defined. I've tried putting the struct definition into a header file, and I've tried removing it or adding it to different parts of the code. I get the same exact errors:
snappy.c:8:13: error: expected identifier or ‘(’ before ‘=’ token
struct fish = {
^
snappy.c:16:26: error: parameter 1 (‘f’) has incomplete type
void catalog(struct fish f)
^
snappy.c: In function ‘main’:
snappy.c:24:12: error: variable ‘snappy’ has initializer but incomplete type
struct fish snappy = {"Snappy", "piranha", 69, 4};
^
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:12: warning: excess elements in struct initializer
snappy.c:24:12: warning: (near initialization for ‘snappy’)
snappy.c:24:17: error: storage size of ‘snappy’ isn’t known
struct fish snappy = {"Snappy", "piranha", 69, 4};
struct fish = { is wrong in struct declaration. It should be struct fish {. Remove = sign.

Confused about compiler errors received using gcc

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

C struct declaration and initialization

I have been following tutorials from the web on creating a struct and then initializing it in main(). From the tutorials I have followed, I have created my own example, which is as follows:
#include <stdio.h>
struct test {
int num;
};
main() {
test structure;
}
However, this does not work:
test.c: In function 'main':
test.c:8: error: 'test' undeclared (first use in this function)
test.c:8: error: (Each undeclared identifier is reported only once
test.c:8: error: for each function it appears in.)
test.c:8: error: expected ';' before 'structure'
But when I change:
test structure;
to:
struct test structure;
the code compiles. Why is this though? From the numerous examples I have looked at it seems that I shouldn't need the 'struct' before 'test structure'.
Thanks for your help/comments/answers.
You were reading C++ examples. In C the type of your structure is struct test not test.
You can get around that by doing
typedef struct test_s
{
int num;
} test;

How can we remove undeclared function error when function is declared with typedef in c?

When i have run the following piece of code:
typedef char *lrfield();
struct lrfields {
char name[26];
lrfield *f;
};
struct lrfields lr_table[] = {
{"pri_tran_code1", pri_tran_code2},
{"sec_tran_code", sec_tran_code},
{"type_code", type_code},
{"sys_seq_nbr", sys_seq_nbr},
{"authorizer", authorizer},
{"void_code", void_code},
{"",0}
};
char *pri_tran_code2()
{
return pri_tran_code;
}
*
*
if(second)
{
for(bp=lr_table; bp->name[0]; bp++)
if(strcmp(bp->name, second)==0)
{
tmpval=bp->f();
break;
}
}
I have got these errors:
error: `pri_tran_code2' undeclared here (not in a function)
error: initializer element is not constant
error: (near initialization for `lr_table[0].f')
error: initializer element is not constant
error: (near initialization for `lr_table[0]')
error: initializer element is not constant
error: (near initialization for `lr_table[1]')
As you can see in the code that i have defined 'pri_tran_code2' above its call. Please help me to solve this error.
Add char *pri_tran_code2(); before you mention this name? Or simply move the whole implementation there. It doesn't matter where you call it, what matters is where you refer to it.
Your declaration is erroneous. To declare a function (function-pointer) type, try this instead:
typedef char *(*lrfield)();

Resources