-3

Why do I keep getting this errors:

struct has no members [-Wpedantic]
‘struct cheque’ has no member named ‘refc’
‘struct cheque’ has no member named ‘valor’

etc. The code is as below


#ifndef _ITEM_
#define _ITEM_ 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define key (a) (a != NULL ? a->refc : "")      
#define less (a,b) (strcmp (a,b)<0)
#define eq(a,b) (strcmp (a,b) == 0)
#define NULLitem NULL

typedef long int* Key;                  

typedef struct cheque {                 
    int valor
    long int refe
    long int refb
    long int* refc

}*Item;                         

Item newItem (int valor, long int refe, long int refb, long int* refc);
void deleteItem (Item a);
void visitItem (Item a);

#endif

EDIT:

Now I'm facing folloeing errors,

Item.c:6:36: error: expected ‘;’, ‘,’ or ‘)’ before ‘refe’ Item newItem (int valor, long item refe, long item refb, long item* refc) ^ Item.c: In function ‘visitItem’:

Item.c:32:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=] printf("refe: %d\n", a->refe); ^

Item.c:33:2: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long int’ [-Wformat=] printf("refb: %d\n", a->refb); The code is as below

item.h

#ifndef _ITEM_
#define _ITEM_ 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define key (a) (a != NULL ? a->refc : "")      
#define less (a,b) (strcmp (a,b)<0)
#define eq(a,b) (strcmp (a,b) == 0)
#define NULLitem NULL

typedef long int* Key;                  

typedef struct cheque {                 
    int valor;
    long int refe;
    long int refb;
    long int* refc;

}*Item;                         

Item newItem (int valor, long int refe, long int refb, long int* refc);
void deleteItem (Item a);
void visitItem (Item a);

#endif

item.c

#include "Item.h"
#include <stdio.h>
#include <stdlib.h>


Item newItem (int valor, long item refe, long item refb, long item* refc)
{

    Item x = (Item) malloc (sizeof(struct cheque));

    x->valor = valor;
    x->refe = refe;
    x->refb = refb;
    x->refc = strdup(refc);

    return x;
}


void deleteItem (Item a)
{
    free(a->refc);
    free(a);

}


void visitItem (Item a)
{

    printf("valor: %d\n", a->valor);
    printf("refe: %d\n", a->refe);
    printf("refb: %d\n", a->refb);
    printf("refc: %ld\n", a->refc);

}

EDIT_v2

Item newItem (int valor, long int refe, long int refb, long int* refc)

printf("valor: %d\n", a->valor);
printf("refe: %ld\n", a->refe);
printf("refb: %ld\n", a->refb);
printf("refc: %p\n", a->refc);

After correcting those mistakes I got the following errors:

Item.c: In function ‘newItem’: Item.c:14:2: warning: implicit declaration of function ‘strdup’ [-Wimplicit-function-declaration]

x->refc = strdup(refc); ^ Item.c:14:10: warning: assignment makes pointer from integer without a cast [enabled by default] x->refc = strdup(refc);

Item.c: In function ‘visitItem’: Item.c:34:2: warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘long int *’ [-Wformat=] printf("refc: %p\n", a->refc);

Edit_ v3

1 problem fixed Correction:

x->refc = refc;

Errors atm:

Item.c: In function ‘visitItem’: Item.c:34:2: warning: format ‘%p’ expects argument of type ‘void *’, but argument 2 has type ‘long int *’ [-Wformat=] printf("refc: %p\n", a->refc);

Miguel
  • 31
  • 1
  • 1
  • 6
  • Essentially down to typos: you're missing plenty of `;`. Also consider replacing your macros with functions as the latter are easier to debug. – Bathsheba May 11 '15 at 13:07

2 Answers2

1

You forgot the semicolons:

typedef struct cheque {                 
    int valor;
    long int refe;
    long int refb;
    long int* refc;    

}*Item;                         
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
1

I think, your code should look like

typedef struct cheque {                 
    int valor;
    long int refe;
    long int refb;
    long int* refc;

}*Item;

with the ;s.


EDIT:

I think, that's another typo.

Item newItem (int valor, long item refe, long item refb, long item* refc)

should read

Item newItem (int valor, long int refe, long int refb, long int* refc)

Then,

printf("refe: %d\n", a->refe);
printf("refb: %d\n", a->refb);
printf("refc: %ld\n", a->refc);

is having wrong format specifiers which in turn invokes undefined behaviour.

The correct format specifier for long int is %ld, and that for long int * is %p.

Also, please do not cast the return value of malloc() and family in C.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Damn, you are right, such a rookie mistake – Miguel May 11 '15 at 13:18
  • @Miguel Never mind, happens. :-) – Sourav Ghosh May 11 '15 at 13:19
  • Thank you :) Can you help me with my new erros :/ ? I am new to pointers, structs and FIFO stuff in C .. – Miguel May 11 '15 at 13:25
  • @Miguel I would love to help, but please do not edit to remove the previous question. Simply __add__ below it your additional queries. Hope you understand. I've already rolled back your latest edit. Please edit accordingly,. – Sourav Ghosh May 11 '15 at 13:30
  • I am sorry, I am new to stackoverflow.. I registered today... Sorry for not following the rules. How should I do then? should I "answer my question" to add a new querie? – Miguel May 11 '15 at 13:31
  • @Miguel This time, I'll do the edits for you. :-) But be sure to take the [tour](http://stackoverflow.com/tour) and read [How to Ask](http://stackoverflow.com/help/how-to-ask) here. – Sourav Ghosh May 11 '15 at 13:35
  • @Miguel updated your question and my answer. :-) – Sourav Ghosh May 11 '15 at 13:39
  • I have taken the tour, thank you for your help and editions I just have one quesiton, where could I add my question that you edited? I tried to put it in a coment but it was too long, should I had to edit like you did? – Miguel May 11 '15 at 13:47
  • @Miguel yes, obviously. Putting up a good question (which people would love to answer) is your job. I just gave an example by doing this edit. You need to do that now onwards. :-) – Sourav Ghosh May 11 '15 at 13:48
  • I have edited with a new question. But my post keeps getting down votes :( and was put as off topic :/ – Miguel May 11 '15 at 14:00
  • Could you help me with edit_v3 edition? I don't know how to fix that problem, the others I already did – Miguel May 11 '15 at 14:22