Re: how to replace a substring in a string using C?



Netocrat wrote:

> > size_t count;

> > count = q - p;
>
> As discussed elsethread,
> under C99 this invokes undefined behaviour when q - p > PTRDIFF_MAX.
> I vaguely recall discussions on the semantics under
> C89, but can't recall the conclusion. Also under C99 count would be
> better declared ptrdiff_t but it appears that you're writing to C89.

Whether or not to declare count ptrdiff_t,
is not really an 89 vs 99 issue.
The type of the expression, (q - p), is ptrdiff_t.
If there's going to be undefined behavior,
it's going to be from that expression.

While there is no PTRDIFF_MAX macro in C89,
there is still a maximum limit to the ptrdiff_t type,
there's just no portable way to tell what is.
I suppose you can assume that it's at least 127.

--
pete
.



Relevant Pages

  • Re: float pointers
    ... >> int main. ... Even though C89 allowed implicit int, ... >> don't declare functions inside other functions - this isn't supported ... Which is why compilers complain, ...
    (comp.lang.c)
  • Re: const array declaration
    ... [I snipped some gcc diagnostics from a previous poster here] ... outside the C code changes it, so that the compiler cannot simply ... "b" are thus not at all constants, but C99 accepts this, just as ... In C89, all aggregate initializers ...
    (comp.lang.c)
  • Re: Planned coverage of CookBook/Matrix, mySituation (was months ago: Requesting advice how to clean
    ... It is better to specify anything that is not C89 is not C89 and ... what it actually is, be it C99, POSIX, MS, GNU or whatever. ... specify things in C99 or whatever as appropriate and appropriately ... to ameliorate whatever is bothering them so much to provoke them to ...
    (comp.lang.c)
  • Re: On the development of C
    ... applied to C89, all of the changes that were made in C99, all of the ... changes that were made in the 3 TCs to C99 - they were non-existent? ... The problem of zero terminated string as the ONLY type ... jacob at jacob point remcomp point fr ...
    (comp.lang.c)
  • Planned coverage of CookBook/Matrix, mySituation (was months ago: Requesting advice how to clean up
    ... It is better to specify anything that is not C89 is not C89 and ... what it actually is, be it C99, POSIX, MS, GNU or whatever. ... specify things in C99 or whatever as appropriate and appropriately ... was to her false allegations, the judge ruled summarily against me ...
    (comp.lang.c)