How to increase bidirectional dereferencing pointer?

In the code that I would normally use:

#include <stdlib.h>

void dref1(char **blah)
{
    (*blah)[0] = 'a';
    (*blah)[1] = 'z';
    (*blah)[2] = 0x00;
}

/* or */

void dref2(char **blah)
{
    char *p = *blah;

    *p++ = 'w';
    *p++ = 't';
    *p = 0x00;
}


int main(void)
{
    char *buf = malloc(3);

    dref1(&buf);
    puts(buf);
    dref2(&buf);
    puts(buf);
    free(buf);

    return 0;
}

My question is whether it is possible / how to dereference the increment pointer directly:

**blah = 'q';       /* OK as (*blah)[0] ? */
(*blah)++;          /* Why not this? */
*((*blah)++) = 'w'; /* ;or. */
...
+5
source share
3 answers

Yes we can. Just check the bedding. I think you have an idea.

The problem with using blahhow you write is that when you return to the main one, not only the contents of the buffer (correctly, how you do it) change, but also the pointer buf. It may not be what you want, and definitely not what you need to free.

Test:

#include <stdlib.h>

void dref1(char **blah)
{
    (*blah)[0] = 'a';
    (*blah)[1] = 'z';
    (*blah)[2] = 0x00;
}

/* or */

void dref2(char **blah)
{
    char *p = *blah;

    *p++ = 'w';
    *p++ = 't';
    *p = 0x00;
}

void dref3(char **blah)
{
    *(*blah)++ = 'w';
    *(*blah)++ = 't';
    *(*blah) = 0x00;
}

void dref4(char **blah)
{
    **blah = 'q';       /* OK as (*blah)[0] ? */
    (*blah)++;          /* Why not this? */
    *((*blah)++) = 'w'; /* ;or. */

}


int main(void)
{
    char *buf = (char*)malloc(3);
    char *buf1=buf;
    dref1(&buf);
    puts(buf);
    dref2(&buf);
    puts(buf);
    dref3(&buf);
    puts(buf);
    puts(buf1);

    buf=buf1;
    dref4(&buf);
    puts(buf);
    puts(buf1);

    free(buf1);

    return 0;
} 
+2
source

, , , *p++ (*p)++ .

, , .

, postfix ++. , , .

* postfix ++. ++ , *.

EDIT:

, blah char **, :

  • , blah char **;
  • *blah char *;
  • **blah char;
  • *(*blah)++ dereferences blah, char *, *p++.
  • **blah++ dereferences blah , , blah.
  • (**blah)++ blah , (*p)++.
+11

I think there is nothing wrong with dref1.

(*blah)[0] = *(*blah)+0 = **blah

so you can write (*blah)[0]='a';

There is no point in doing it (*blah)++;. You simply increase and lose the address returned malloc.

0
source

All Articles