50

In the course of my reading I came accross WG14 Defect Report #51 written in 1993 (or perhaps 1893, they left off the century and millennium). In the code sample there, apparently an operator spelled ->> is used on a pointer to a struct. I can't find it in any operator precedence tables I've found, so I am wondering, is or was it ever an operator, and if so, what does (or did, as the case may be) this operator do?

At first I thought it was a typo, but it is reproduced twice more in the text and another time in the code sample in the response to the question, and I have a hard time believing it just slipped past at least two C experts without being noticed, when it jumped out at a novice like me. It's also at the focal point of the code, very easy to notice, and was never corrected.

Here is the code with added indentation:

#include <stdlib.h>

struct A {
    char x[1];
};

main()
{
    struct A *p = (struct A *) malloc(sizeof(struct A) + 100);
    p->>x[5] = '?';  /* This is the key line [for both them and us] */
    return 0;
}

I tried to compile this code with both a C and C++ compiler and it failed to parse in either one. Perhaps this was some operator in an early version of C that isn't used any more?

This feels suspiciously like the What is the name of this operator: "-->"? question, but I don't think this is a combination of two other operators, I don't see how it can be divided up and be valid.

Community
  • 1
  • 1
Seth Carnegie
  • 73,875
  • 22
  • 181
  • 249
  • 16
    I have a feeling that's a typo. – R. Martinho Fernandes Oct 24 '12 at 13:27
  • @R.MartinhoFernandes so both the questioner and the answerer copy/pasted `p->>x[5]` twice and then replied with a code sample without noticing it? I find that hard to believe. – Seth Carnegie Oct 24 '12 at 13:28
  • Oh, only now have I looked closely at the paper. That doesn't appear only once or twice. Either that's one heck of a copy-paste accident trainwreck or... I have no freaking idea. – R. Martinho Fernandes Oct 24 '12 at 13:30
  • 4
    @SethCarnegie: I agree that it's hard to believe, but I find it easier to believe than that as recently as 1993 C had a `->>` operator since lost to historians. That Andrew Koenig is an immortal who was alive and commenting on the C standard in 1893 is somewhere between the two. Maybe he had a great-grandfather? ;-) – Steve Jessop Oct 24 '12 at 13:35
  • 2
    I am glad, it's not a [trigraph](http://en.wikipedia.org/wiki/Digraphs_and_trigraphs#C). – Tilman Vogel Oct 24 '12 at 13:36
  • @Steve Everyone knows immortality does not exist. He's just a time traveler. – R. Martinho Fernandes Oct 24 '12 at 13:36
  • @R.MartinhoFernandes: d'oh, good call. I wonder if he reads his vanity Google alerts. – Steve Jessop Oct 24 '12 at 13:36
  • @TilmanVogel that's what I thought too, but why would they make a trigraph out of characters that the operator they're trying to graph is composed of? – Seth Carnegie Oct 24 '12 at 13:37
  • @Seth, maybe it's a well-thought hoax made specifically to fool people 20 years after? – SingerOfTheFall Oct 24 '12 at 13:37
  • @SteveJessop yeah, 1993 isn't that long ago, but it's just very hard to call it quits here and say that it was just a cosmic typo. – Seth Carnegie Oct 24 '12 at 13:38
  • Other defect reports from 1993 doesn't mention this operator at all... – md5 Oct 24 '12 at 13:42
  • 2
    It's the 'struct hack' operator :) Notice you're accessing the element with index 5 of an array with a single element: which is (arguably) Undefined Behaviour. Using `->>` makes the UB more visible – pmg Oct 24 '12 at 13:43
  • 41
    Simply, this code is written in 2093, so `operator->>` still not invented. – Lol4t0 Oct 24 '12 at 13:43
  • 3
    We should ask the author of [this SO post](http://stackoverflow.com/a/1110435/637284) as he uses `->>` :) – halex Oct 24 '12 at 13:51
  • Note that if there is a bug, it doesn't show up in http://www.open-std.org/jtc1/sc22/wg14/www/docs/dr_066.html – slaphappy Oct 24 '12 at 13:52
  • @Lol4t0 so it's the TARDIS operator? – Seth Carnegie Oct 24 '12 at 13:53
  • We know that when Jon Skeet points to `null`, the `null` shakes in fear. So maybe _this_ is the way Jon did it back in 1993 under a pseudonym? – SingerOfTheFall Oct 24 '12 at 13:53
  • Why couldn't it be a typo? Have you never been so focused on a piece of code that you miss something which would seem obvious to an external viewer? `Why does this foor loop only run once? Dude it's an if, not a for loop`. – user703016 Oct 24 '12 at 13:53
  • 1
    @Cicada Well it's a typo in the part of the code that they were all focused on, _and_ two people missed it, _and_ it was never corrected, _and_ there is a big comment saying `/* This is the key line */` on the very line the "typo" is on (I didn't put that there, they did), and... etc. Did you have a hard time noticing it? I couldn't help but notice it the first time I saw it. I can't lay it at the feet of bad reading this time, it's just too improbable. – Seth Carnegie Oct 24 '12 at 13:58
  • 2
    @SethCarnegie Yes, I noticed it right away and I *do* agree with you that it seems *very* improbable for it to be a typo. However, it seems *even more unlikely* to be something else: it behaves *just like* operator `->` and no other defect report contains `->>`, not to mention nobody has ever seen this operator before. So while it seems indeed *extremely improbable* to be a typo, it still is (IMO) *the most probable* solution. – user703016 Oct 24 '12 at 14:04
  • 6
    "Once you eliminate the impossible, whatever remains, no matter how improbable, must be the truth." -Arthur Conan Doyle – Adam Rosenfield Oct 24 '12 at 14:07
  • @Cicada if it's not another operator (which I agree is also improbable) then I would think it was not a typo but some error in the transcription (?) process after it was written. – Seth Carnegie Oct 24 '12 at 14:08
  • 1
    There is a similar issue in DR 42, where the greater than sign is doubled. – Vaughn Cato Oct 24 '12 at 14:21
  • Maybe someone used a very very bad Makro and a #define somewhere else in the project !? – TWE Oct 24 '12 at 14:23
  • @VaughnCato then it's some bug in the software or transcription process, if you put that down I'll mark it as the answer. – Seth Carnegie Oct 24 '12 at 14:24
  • Looks like the <-- operator, while(0<--p)... he he – Valmond Oct 24 '12 at 15:01
  • Ask the original submitter: pjp@plauger.com – lesderid Oct 30 '12 at 19:43
  • @TWE It is not possible to write a macro that changes the problem tokenization in this case. (Macros can only be identifiers. You might think writing `#define p p_-##` would cause `p-` to be retokenized as `p_ --`, but `##` does not work that way.) – zwol Feb 14 '14 at 23:28

3 Answers3

29

It looks like a problem in the transcription process. There is a similar problem in DR 42, where the greater than sign is doubled: http://www.open-std.org/jtc1/sc22/wg14/docs/rr/dr_042.html

Vaughn Cato
  • 63,448
  • 5
  • 82
  • 132
23

I learned C in 1992, and I'm 100% certain there was no such operator back then.

From the context, p->>x[5], we can deduce that it appears to do exactly the same thing as the more familiar arrow operator, ->. It is therefore likely to be a typo.


Alternatively, it could be an encoding issue in transcribing the code into HTML. If you look at the source to that page, you can see it's got a strange mixture of escape codes and literal < and > characters:

<TT><B>#include &lt;stdlib.h><BR>
Graham Borland
  • 60,055
  • 21
  • 138
  • 179
1

This does seem likely to have been a transcription error, but I think it would be useful to write out how a real C compiler would interpret this construct, anyway, just to make clear that it isn't a clever trick. The first thing it's important to know is this sentence, from C11 §6.5.4p4 (technically, N1570; this language is unchanged since C89, although the section number was probably different; emphasis mine):

If the input stream has been parsed into preprocessing tokens up to a given character, the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token.

That means the six-character string " p->>x" must be tokenized as p -> > x, not p - >> x or p - > > x. (It doesn't actually matter in this case, it would be a syntax error either way, but this rule can be the difference between a program parsing as intended, and not; the standard gives the example x+++++y, which is interpreted as x++ ++ +y, not as x++ + ++y, even though only the latter is a well-formed expression.)

The next thing to know is simply that the right-hand argument of the -> operator must be an identifier, per the grammar rules for postfix-expression in §6.5.2. Obviously > isn't an identifier, so we have a definite syntax error.

zwol
  • 135,547
  • 38
  • 252
  • 361