Results 1 to 10 of 10
I have my own declared types and I have to operate with lists of items. I wish to write a one macro, instead of using a function for each type. ...
- 04-13-2010 #1Linux Newbie
- Join Date
- Apr 2010
- Location
- Novosibirsk, Russia
- Posts
- 136
Append item to a list ( C macro )
I have my own declared types and I have to operate with lists of items. I wish to write a one macro, instead of using a function for each type. It's intended to be like this:
but when I call the macroCode:#define ____append_to_list(head, item, type) \ type* ptr; \ for ( ptr = head; ptr->next != NULL; ptr = ptr->next ) \ {} \ item->prev = ptr; \ ptr->next = item
and try to compile it, I get gcc errorCode:____append_to_list(list_head, new_item, Item); /* list_head and new_item is a pointers of a 'Item' type */
how can I use it correctly?...Code:error: expected expression before 'Item'
- 04-13-2010 #2Just Joined!
- Join Date
- Apr 2010
- Location
- Ankara
- Posts
- 10
I tested macro in my machine. There is no problem with macro nor get compilation error, but probably your structure declaration is erroneous. Here is my code:
typedef struct list {
struct list* prev;
struct list* next;
} list;
int main(int argc, char **argv) {
list* head = (list *) malloc(sizeof(list));
list* item = (list *) malloc(sizeof(list));
____append_to_list(head, item, list);
return 0;
}
- 04-13-2010 #3Linux Newbie
- Join Date
- Apr 2010
- Location
- Novosibirsk, Russia
- Posts
- 136
Struct definition
hmm, yes, my definition differs from yours, here it is:
but in other parts of code using Item type it's not a problem. Maybe it's because of compiler options?... I use GCC version 6.8 with no additional compiling or preprocessing options. I'll try to delete the underline sigil first, of course...but still interested in this phenomenonCode:typedef struct _Item { /* ... */ struct _Item *prev; struct _Item *next; } Item;
- 04-14-2010 #4Just Joined!
- Join Date
- Apr 2010
- Location
- Ankara
- Posts
- 10
its really interesting. I got no errors with underline or without. its also should not be like that logically.
I compiled as : gcc test.c -o test
my gcc version: 4.4.1 (I think oldie one
)
- 04-14-2010 #5Linux Newbie
- Join Date
- Apr 2010
- Location
- Novosibirsk, Russia
- Posts
- 136
))
it seems that my head just has "buffer overflow"
I tried to call ____list_append() but macro was defined as ____append_to_list() ... Oh, just words playing, but its price appeared high
)
there is another problem too - it's because of
when I use the macro once, that's no problem. But if i call it two or more times in the same namespace (eg. in a function), compiler says that the 'ptr' is redeclared. How can I fix it?Code:... type* ptr; \ ...
- 04-14-2010 #6Linux Newbie
- Join Date
- Apr 2010
- Location
- Novosibirsk, Russia
- Posts
- 136
I suppose I should not declare pointer inside of macro - it seems that I need just declare a static pointer for each data type and just pass them to my macro...
- 04-15-2010 #7
Enclose the body of the macro in {} to make the "ptr" variable be local to that block. This allows you to have that variable defined multiple times locally, so you can have multiple calls to the macro. Like this:
Code:#define ____append_to_list(head, item, type) \ { \ type* ptr; \ for ( ptr = head; ptr->next != NULL; ptr = ptr->next ) \ {} \ item->prev = ptr; \ ptr->next = item; \ }
- 04-15-2010 #8
- 04-16-2010 #9Just Joined!
- Join Date
- Apr 2010
- Location
- Ankara
- Posts
- 10
- 04-18-2010 #10Linux Newbie
- Join Date
- Apr 2010
- Location
- Novosibirsk, Russia
- Posts
- 136
Yeah, it works, thanks!


Reply With Quote
