0

In the following segment of code:

if (buffer  + strlen(buffer) >= len -1) beep();

note: len is an int, buffer is a pointer to char.

I don't understand how would someone add buffer (a pointer) to the strlen() of a string and compare it to len. Can any one please help me. note: the actual code link is http://www.finseth.com/craft/#intro.1.4 any help will be greatly appreciated.

4
  • the line of code in the 'if' statement is nonsence. suggest: ''if( strlen(buffer) > (len-1)) beep(); ' However, by the time this condition is 'true', the buffer would be past the end of the 'text' literal, resulting in undefined behaviour and can/will lead to a seg fault event Commented May 19, 2015 at 23:52
  • note that the 'if' statement is depending heavily on the presidence of the C operators. It would be much clearer to humans if another set or two of parens were incorporated. Commented May 19, 2015 at 23:59
  • @user3629249 it's common knowledge that arithmetic operators have higher precedence than relational, I doubt anyone looks at this and thinks it might be buffer + (strlen(buffer) >= len) - 1 Commented May 20, 2015 at 0:12
  • To me the code at that side looks somewhat ouitdated, but that might be due to the (nowadays) uncommon formatting. For the line in question, I would suspect some pre-ANSI (aka K&R) hacks or a mixture. Anyway it is non-standard and even if len was a proper pointer, it's name is not really self-explanatory (for instance, it would have to point to the same array). With that line, I would be really careful on the rest of the code. Commented May 20, 2015 at 0:43

2 Answers 2

3

This code is illegal. A pointer may not be compared to an integer (other than a constant 0). The compiler should have generated an error message.

Some compilers may generate "only" a warning in the default configuration, and perform a nonsensical comparison at runtime, but you should treat this as an error.

You could report this bug to the author of this page; although if basic compilation errors get through their QA process I hate to think what other mistakes might also be present.

Sign up to request clarification or add additional context in comments.

Comments

0

It's called pointer arithmetic, it's essentially the same as

if (&buffer[strlen(buffer)] >= len - 1)

which is a very ugly line of code in both versions.

Why do they compare it to len - 1 is a mistery, unless len is overwritten from the initial value, or the programmer knows exactly what the address of "text" is, which would depend on the compiler AFAIK.

2 Comments

would you please take a look to the actual code? it is from a book named the craft of text editing and the link to the code is: finseth.com/craft/#intro.1.4
In addition to being ugly, it's also a bug. buffer + strlen(buffer) is a pointer, but it's being compared to the integer len - 1, which is unlikely to result in anything meaningful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.