Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

List at compile time

Name: Anonymous 2010-10-22 4:03

Hi /prog/.

I need some help, i want to make a list, but i want to make it at compile time using preprocessor macros.

I need to do something like this:

typedef struct shit {
char *name;
int num;
void *next;
} ass:

ass dong = {"dong", 0, NULL};
ass dick = {"dick", 1, &dong};

But i need to do it using macros so i can just safely append them to each other.

Help me

Name: Anonymous 2010-10-22 4:15

item items[]={
    {  0, "why",},
    {  1, "don't",},
    {  2, "you",},
    {  3, "just",},
    {  4, "read",},
    {  5, "K&R",},
    {999, "you imbecile",},
}

Name: Anonymous 2010-10-22 4:20

>>2
You can't do compile time list or struct initialization in C89 AFAIK. Because nobody has ever read the C99 standard, much less implemented it, you are going to have to use C++ if you want to do this.

Name: Anonymous 2010-10-22 4:21

>>2
No, the idea is that i do not do them right away, but that they will be defined later troughout all the files and that i dont know how many i will have.

Name: Anonymous 2010-10-22 4:31

>>4
Write a program to parse your files and generate code for you. There is no preprocessor magic that will allow you to do this in pure C. Or you can use C++ constructors for this (store pointer to last node of list in global, and append a node to it in constructor of some dummy class), but then order will be undefined.

Name: Anonymous 2010-10-22 4:36

>>5
There is no preprocessor magic that will allow you to do this in pure C

I was only asking for this since i hoped /prog/ would know a "pure C" way of doing this, it try to avoid external programs. But its not a problem for me doing this without C.

Name: Anonymous 2010-10-22 4:38

/* list.h */
ASS_NODE(dong)
ASS_NODE(dick)
ASS_NODE(cheese)


/* shit.c */

#define _QUOTE(x) #x
#define QUOTE(x) _QUOTE(x)

#define ASS_NODE(name) NODE_##name
enum {
#include "list.h"
  LAST_NODE
};
#undef ASS_NODE

typedef struct shit {
  char *name;
  int num;
  struct shit *next;
} ass;

#define ASS_NODE(name) { QUOTE(name), NODE_##name, NODE_##name ? array + NODE_##name - 1 : NULL },
ass array[] = {
#include "list.h"
  { 0, 0, 0 }
};
#undef ASS_NODE

ass *ass_list = array + (LAST_NODE - 1);

Name: Anonymous 2010-10-22 4:39

struct node {
    int value;
    struct node *next;
};

struct node list[] = {
    {0, &list[1]},
    {1, &list[2]},
    {2, &list[3]},
    {3, NULL},
};

Name: Anonymous 2010-10-22 4:39

>>6
Preprocessors for each .c are completely independent from each other. What you want is to send information about "dong" from preprocessor of one .c file to some other, so that it can put a reference to dong into structure somewhere, but there is no way this can work.

Name: Anonymous 2010-10-22 4:55

If you know what the list contains at compile time, then why not store it in an array?

Name: Anonymous 2010-10-22 5:00

>>10
Unfortunetly, i try to do this with functions, instead of ints.
I didnt try to confuse things more than they already are.

What i precisely want is a list with nodes whose first element is a name and the second a function pointer.

Its for a shell btw.

Name: Anonymous 2010-10-22 5:01

>>11
Enjoy you O(n) worst case

Name: Anonymous 2010-10-22 5:04

>>12
Enjoy your misunderstanding that the general case O(n/2) is O(n).

Name: Anonymous 2010-10-22 5:05

>>11
What stops you from making a list like the one in >>2?

Name: Anonymous 2010-10-22 5:10

>>14
I cant define function in an enum, atleast that is what i heard.

I thought about giving up programming, im just so shit at this.

Name: Anonymous 2010-10-22 5:19

/* functions.h */
FUNCTION(foo, "Foo routine")
FUNCTION(bar, "Bar procedure")
FUNCTION(baz, "Baz subroutine")
FUNCTION(quux, "Quux function")


/* functions.c */

#define FUNCTION(name, pretty_name) int name (int arg);
#include "functions.h"
#undef FUNCTION

typedef struct fn_node {
  const char *name;
  int (*fn) (int);
  struct fn_node *next;
};

#define FUNCTION(name, pretty) FN_##name ,
enum {
#include "functions.h"
  MAX_FUNCTION
};
#undef FUNCTION

#define FUNCTION(name, pretty) { pretty, name, FN_##name + 1 == MAX_FUNCTION ? 0 : array + FN_##name + 1 } ,
static struct fn_node array[] = {
#include "functions.h"
  { 0 }
};

struct fn_node *list = array;

Name: Anonymous 2010-10-22 5:48

>>13
wat

Name: Anonymous 2010-10-22 6:07

>>15
If you want to map strings to functions, just store pairs in an array, qsort it, and use bsearch.

Name: Anonymous 2010-10-22 6:16

>>15
What does enum have to do with this?

Name: Anonymous 2010-10-22 6:19

>>18
I would use a trie. This would also aid in tab-completion

Name: Anonymous 2010-10-22 6:47

>>19
I missred that, silly me.

Name: Anonymous 2010-10-22 8:28

>>21
You misspelled misread.

Name: Anonymous 2010-10-22 8:42

>>22
He added an extra s, but for internet communication i support using "red" rather than "read" for the past tense of "to read" as it aids clarity.

Name: Anonymous 2010-10-22 9:17

>>23
aids
Back to the imageboards, please!

Name: Anonymous 2010-10-22 17:28

He said aids not AIDS

Name: Anonymous 2010-10-24 3:39

I got it working with your help, thank you.
While my solution is not as good as i want it to be it gets the job done.

Name: Anonymous 2011-03-06 8:20

That would be so easy in Lisp.

Name: Anonymous 2011-03-06 8:35

>>28
That is why you've to use C++ and Java. Jews want you to suffer.

Name: Anonymous 2011-03-06 9:51

By the way this can be done much more elegantly in C99.


struct llist { atom a; struct llist* next;};
#define cons(x,y) (struct llist[]){{x,y}}
struct llist *lst=cons(a1, cons(a2, co...ns(aN,NULL) ... ));


reference: http://www.run.montefiore.ulg.ac.be/~martin/resources/kung-f00.html

Name: Anonymous 2011-03-06 10:00

>>30
Greenspun's tenth rule.

Name: Anonyous 2011-03-06 14:48

ffff

Don't change these.
Name: Email:
Entire Thread Thread List