Explanation of ? operator missing in reference guide

L

luserXtrog

Skybuck said:
The intention ofcourse is to execute different code based on the value of A
like a case statement in pascal or a switch in C.

A = 4;
A ? B = 1; C = 2; D = 3; E = 4; F = 5;
A ? Init(); Work(); Draw(); Next(); CleanUp();

I would write that, this way:

     func[A]();

/* BEGIN new.c */

#include <stdio.h>

void Init(void);
void Work(void);
void Draw(void);
void Next(void);
void CleanUp(void);

int main(void)
{
     void (*func[])(void) = {Init,Work,Draw,Next,CleanUp};
     size_t A;

     for (A = 0; A != sizeof func / sizeof *func; ++A) {
         func[A]();
     }
     return 0;

}

void Init(void)
{
     puts("Init");}

void Work(void)
{
     puts("Work");

}

void Draw(void)
{
     puts("Draw");

}

void Next(void)
{
     puts("Next");

}

void CleanUp(void)
{
     puts("CleanUp");

}

/* END new.c */

Nice.
How about:

#include <stdio.h>
int Init(int i) { puts( "Init"); return i+1;}
int Work(int i) { puts( "Work"); return i+1;}
int Draw(int i) { puts( "Draw"); return i+1;}
int Next(int i) { puts( "Next"); return i+1;}
int CleanUp(int i) { puts("CleanUp"); return -1;}
typedef int (*ifi)(int);
int main(void) {
int A = 0;
while((A=A[(ifi[]){Init,Work,Draw,Next,CleanUp}](A))!=-1)0xF00L;
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top