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

char (*(*x())[])()

Name: Anonymous 2012-01-28 18:16

"char (*(*x())[])()
x: function returning pointer to array[] of pointers to function returning char" - K&R, page 122.

Because C is concise and elegant.
Seriously, I am a fan of the language, but this is unforgivable.

Name: Anonymous 2012-01-28 18:18

Use assembly, problem solved.

Name: Anonymous 2012-01-28 18:21

>>2
Look, I am sure there is a better syntax for the same concept than this monstrosity. It might be a bit longer but it does not have to be assembler-long. The above appears like deep magic to people who are not paladins of C.

Name: Anonymous 2012-01-28 18:21

Use assembly, problem solved.

x: function returning pointer to array[] of pointers to function returning char

becomes

x: function returning pointer

Name: Anonymous 2012-01-28 18:21

It looks confusing at first but the rules for reading it are very simple.
Why do you think it's "unforgivable"?

Name: Anonymous 2012-01-28 18:24

>>5
I feel it betrays the very spirit of the language. It is the ugly side of it - the one that eventually lead to the birth of sepples. C should be simple, elegant, and powerful. It is a language that, having studied it for a few hours, you can guesstimate the effect of almost every line of code. Not so for this line, which is counter-intuitive.

Name: Anonymous 2012-01-28 18:24

lets see:

type(x) = function((), array(pointer(function((), char)), _))

Name: Anonymous 2012-01-28 18:26

typedef char (*(*x_type())[])();
x_type x;

PROBLEM SOLVED

Name: Anonymous 2012-01-28 18:29

>>6

well, it's meaning is very consistent. It's not like putting the int inside of operator++(int), which doesn't make any sense, other than for convenient parsing.

You got a function returning char?

char g();

Now an array of pointers to the thing that g's type is? substitute it in where the letter g is:

char (*h[])();

Now a pointer to the thing that h is? put a (*k) where h is:

char (*(*k)[])();

Now a function taking no arguments that returns the type that k is? put a f() where k is:

char (*(*f())[])();

Name: Anonymous 2012-01-28 18:32

>>6
But it is simple and arguably also elegant.

having studied it for a few hours
It takes 30 sec to learn the rules for reading type signatures of arbitrary complexity. Start within the innermost parentheses around the name, postfix before prefix, left associative for postfix, right associative for prefix.

Name: Anonymous 2012-01-28 18:34

>>4
x: function returning pointer to array[] of pointers to function returning char
becomes
x: shit, nigga! I'll have to keep track of types manually and rewrite the function every time I port it to a new architecture

Name: Anonymous 2012-01-28 18:41


<declaration>
 <name>
  <identifier>
   f
  </identifier>
 </name>
 <type>
  <function>
   <argument_list>
   </argument_list>
   <return_type>
    <pointer>
     <destination_type>
      <array>
       <item_type>
        <pointer>
         <destination_type>
          <function>
           <argument_list>
           </argument_list>
           <return_type>
            <primitive>
             char
            </primitive>
           </return_type>
          </function>
         </destination_type>
        </pointer>
       </item_type>
       <length>
       </length>
      </array>
     </destination_type>
    </pointer>
   </return_type>
  </function>
 </type>
</declaration>

Name: Anonymous 2012-01-28 18:41

>>9
>>10
I guess I was just overly surprised to see something like this in a language that otherwise avoids such complicated-looking syntax. I realize I am overreacting. I consider the language beautiful, which is why this blemish shocked me.

>>11
I laughed aloud. Good joke.

Name: Anonymous 2012-01-28 18:51

>>13

yeah, it's a consequence of being able to arbitrarily nest your type expressions. Also:


return f(x + y && z | 4 + 6 * 6 & 2 % 34 / t | 90 ? 103 * h(x, y + x) / g(x) : 1 - jj + kk - /* 23 + 45 & t * x */ ****k * k/ *k);

Name: Anonymous 2012-01-28 19:15

C supports something that is not available for 99% of programming languages and you are angry because it is not elegant?

also it is not mean to be elegant, code in brainfuck if you want beauty

Name: Anonymous 2012-01-28 19:22

>>15
The rest of the language has spoiled me. I expected more from it.

Name: Anonymous 2012-01-28 19:37

C is riddled with undefined behaviour for even basic things.  Learn x86 assembly instead.

Name: Anonymous 2012-01-28 19:41

>>17
C++ is worse.

Name: Anonymous 2012-01-28 19:43

Also, I forgot to thank
>>9
and
>>10

Together your explanations made everything clear. So, thanks.

Name: Anonymous 2012-01-28 19:47

>>18
C++
*puke*

Name: Anonymous 2012-01-29 0:29

>>17
http://faydoc.tripod.com/cpu/idiv.htm
ID     unaffected     DF     unaffected
VIP     unaffected     IF     unaffected
VIF     unaffected     TF     unaffected
AC     unaffected     SF     undefined
VM     unaffected     ZF     undefined
RF     unaffected     AF     undefined
NT     unaffected     PF     undefined
IOPL     unaffected     CF     undefined
OF     undefined

Name: Anonymous 2012-01-29 4:41

if we have

struct point {
    int x;
    int y;
};

struct rect {
    struct point pt1;
    struct point pt2;
};

struct rect r, *p = &r;


then these four expressions are equivalent:

&r.pt1.x
&r.pt1
&p->pt1.x
&p->pt1
&(*p).pt1.x
&(*p).pt1


minimalism, yay!

Name: Anonymous 2012-01-29 4:42


Lisp   | C/C++
-------|-----------------------------------------------------
format | printf fprintf sprintf snprintf vsprintf vfprintf vsprintf vsnprint
       | asprintf vasprintf dprintf vdprintf wprintf putchar fputchar putwchar
       | putc fputc fputwc puts fputs fputws write fwrite
       | ostream ofstream streambuf streingstream ostringstream stringbuf
       | fstream filebuf ios ios_base cout cerr std clog << endl ends hex oct
       | boolalpha dec flush internal setw noboolalpha noshowbase noshowpos left
       | right resetiosflags scientific setbase setfill setiosflags setprecision
       | showbase showpoint showpos skipws unitbuf nouppercase uppercase ws

Name: Anonymous 2012-01-29 5:16

>>23
10/10. Lisp actually has quite extensive print functions too, to which format can expand to, although if you don't want to, you don't have to use them

Name: Anonymous 2012-01-29 6:05

Who the fuck needs a function returning a pointer to an array of pointers to functions returning chars anyway? Code like this should never be written in the first place.

Name: Anonymous 2012-01-29 6:11

>>25
Maybe not in C, but it makes sense in some high-level languages.

Let me guess an example usage, a function when called returns an array of locale-specific text functions, thus you'd call the function once, based on a global (since no argument was given in >>1), it retrieves the current selected locale, and gives you an array of functions which return all the translated strings (for that locale). Why functions? Maybe some of them could be date/time-like dynamically generated content.

Name: Anonymous 2012-01-29 12:21

>>26
Without closures there is very little point in C. If I did need to return an array of functions I'd probably use an array of pointers to void and cast the individual function pointers closer to the call site.

If that was a bad idea for some reason I'd make a type for for the function pointer.

Name: Anonymous 2012-01-29 12:36

I have to say being able to read that almost feels like a party trick. Not very useful for anything but impressing the ladies.

Name: kodak_gallery_programmer !!qmiXqQhekkGXVVD 2012-01-29 12:38

>>26
I've seen a lot of low level network code use this method.

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