14

I have a code like this:

int quotient = 100/*ptr; 

where ptr is a pointer to interger.

But it's taking /* as the comment.
How can I make the meaning of divide by pointer dereference value? What extra special character I have to put to have this meaning?

Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
Sreeraj Chundayil
  • 5,548
  • 3
  • 29
  • 68
  • 18
    This question appears to be off-topic because OP should know better and just put spaces around binary arithmetic operators. – Griwes Jan 02 '15 at 13:21
  • if putting a space between the '/' and the '*' does not work, then put parens around the divisor, like so: 'int quotient = 100/(*ptr);' – user3629249 Jan 02 '15 at 13:54
  • 1
    I think off-topic tag is improper. But I am OK with something like,OP hasn't done enough research or so:) – Sreeraj Chundayil Jan 02 '15 at 18:44
  • 2
    I agree that this shouldn't be off-topic, because while it is technically a typographical error, *it is very likely to help future readers.* – RamenChef Dec 21 '17 at 19:43

4 Answers4

34

This happens because language tried to reuse the tokens. (* in this case)

Solution is to put a space between / and * to beat maximal munch.

int quotient = 100 / *ptr;

Another way is to add a parenthesis or use another local variable:

int quotient = 100/(*ptr);
Cœur
  • 37,241
  • 25
  • 195
  • 267
Mohit Jain
  • 30,259
  • 8
  • 73
  • 100
25

First, you can replace *ptr with ptr[0], as both have the same semantics:

int quotient = 100/ptr[0];

And since array indexing is commutative, you can swap the operands:

int quotient = 100/0[ptr];

To the casual reader, this may look like division by zero, but of course [] has higher precedence than /. You may want to put a space there, just in case:

int quotient = 100/0 [ptr];

Congratulations, you now have a job for life!

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • 17
    `[` and `]` may be difficult to maintain for some people, so I suggest using `<:` and `:>` instead. –  Jan 02 '15 at 14:36
  • 1
    @righrfold why not replace the latter one with `??)` so you have `int quotient = 100/0 <: ptr??)`. so even C programmers can read it. – 12431234123412341234123 Dec 20 '17 at 20:37
13

Change it to this:

int quotient = 100/(*ptr); 

or

int quotient = 100/ *ptr;

/* together is used for multi-line comments in almost all languages I know until now.

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Sadique
  • 22,572
  • 7
  • 65
  • 91
13

C and C++ use maximal munch rule to parse the tokens. The longest valid match string after a token will be the next token.

Therefore in int quotient = 100/*ptr;, /* will be a single token instead of two tokens / and *. This is an undesirable effect of the rule.

In some situations, "maximal munch" leads to undesirable or unintuitive outcomes. For instance, in the C programming language, the statement x=y/*z; (without any whitespace) will probably lead to a syntax error, since the /* character sequence initiates a (unintended) comment that is either unterminated or terminated by the end token */ of some later, unrelated actual comment (comments in C do not nest). What was actually meant in the statement was to assign to the variable x the result of dividing the value in y by the value obtained by dereferencing pointer z; this would be perfectly valid (though not very common) code. It can be stated by making use of whitespace, or using x=y/(*z);.

https://en.wikipedia.org/wiki/Maximal_munch#Drawbacks

To fix this you just need to add a space, new line or another dummy token (like a comment) to separate / and *. You can also surround the expression by brackets like in the other answer

int quotient = 100/ /* this will work */ *ptr;
int quotient = 100/
    *ptr; // this will also work

A similar question: Why doesn't a+++++b work in C?

Community
  • 1
  • 1
phuclv
  • 37,963
  • 15
  • 156
  • 475
  • 1
    `int quotient = 100//* this works in C89 but not in C++ and C99+ */*ptr;` – phuclv Apr 11 '17 at 14:15
  • 1
    @DonaldDuck did you read my answer? `To fix this you just need to add a space, new line or another dummy token (like a comment) to separate / and *` – phuclv Dec 22 '17 at 02:25