# Simple question

- Thread: https://externals.io/message/3051
- Original archive: https://news-web.php.net/php.internals/3051
- Started by Oriol on 2003-06-30T16:13:04+00:00
- Messages: 2
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #3051 — Oriol — 2003-06-30T16:13:04+00:00

URL: https://externals.io/message/3051#3051

Hello! I'm trying to write a trim function which expects a zval* of type
string, as follows:

int zstrtrim(zval *z)
{
    char *ptr1 = Z_STRVAL_P(z), *ptr2;
    if (Z_STRLEN_P(z) == 0) return;
    ptr2 = &ptr1[Z_STRLEN_P(z)-1];
    while (*ptr1 != 0)
    {
        if (*ptr1 > ' ') break;
        ptr1++;
    }
    while (ptr2 > ptr1)
    {
        if (*ptr2 > ' ') break;
        ptr2--;
    }
    ZVAL_STRINGL(z, ptr1, ptr2 - ptr1 + 1, 1);
}

    However, the last line gives me a segmentation fault. Why? How can I
change the value of the input zval string?
    Thank you for your patience!

    Oriol

---

## #3052 — Andrey Hristov — 2003-06-30T16:52:04+00:00

URL: https://externals.io/message/3051#3052 · In reply to: #3051

Oriol,
try valgrind (aka memory checker) and you will hopefully find where your
problem resides

Andrey

----- Original Message -----
From: "Oriol" <omagrane@anthill.es>
To: <internals@lists.php.net>
Sent: Monday, June 30, 2003 7:13 PM
Subject: [PHP-DEV] Simple question


>
>     Hello! I'm trying to write a trim function which expects a zval* of
type
> string, as follows:
>
> int zstrtrim(zval *z)
> {
>     char *ptr1 = Z_STRVAL_P(z), *ptr2;
>     if (Z_STRLEN_P(z) == 0) return;
>     ptr2 = &ptr1[Z_STRLEN_P(z)-1];
>     while (*ptr1 != 0)
>     {
>         if (*ptr1 > ' ') break;
>         ptr1++;
>     }
>     while (ptr2 > ptr1)
>     {
>         if (*ptr2 > ' ') break;
>         ptr2--;
>     }
>     ZVAL_STRINGL(z, ptr1, ptr2 - ptr1 + 1, 1);
> }
>
>     However, the last line gives me a segmentation fault. Why? How can I
> change the value of the input zval string?
>     Thank you for your patience!
>
>     Oriol
