Thread: using ++ and --

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    4

    Question using ++ and --

    Hi all!

    I have a question: in standard C would it be possible to use the following:

    ++i--;

    or as I would like to use it:

    x ( a[ ++i-- ] ) ;

    where i and a global vars, and x calls itself recursively.

    I tried with 2 compilers, but both found it to be erroneous. But is it erroneous in standard C?

  2. #2
    Registered User codingmaster's Avatar
    Join Date
    Sep 2002
    Posts
    309
    No it doesn't work, why do u wanna write such code???

    your code is not valid C!!!

    u can use something like:

    int x=0;
    ++x,x--;

    but at the end x=x+(1-1);
    so it's equal to x=x

    there is no sense for writing such code, or gimme an example, or write down what u wanna do......

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    4
    Thank you!

    the example was that i wrote above, calling a function with a global variable so that fuction is called with the increased value, but when it returns, global variable would be the same as you wrote.

    Of course, I can do it with one more line, but I was wondering...

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    Doesn't this do what you want?

    x( a[i+1] );

    no need for an extra line...
    DavT
    -----------------------------------------------

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    4
    no, it wouldn't do what it supposed, and actually my version is bad, too...

    the correct, what i really need is:

    i++;
    x ( a [ i-1 ] );
    i--;

    so, call with i-th element of "a", but increase i before calling and decrease after.

  6. #6
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    ok, so explain how that is different from:

    x( a[i] );

    why do you feel the need to increment i before the call and decrement it afterwards? am i missing something else that you are trying to acheive as a side effect? consider this:
    Code:
    #include <stdio.h>
    
    void x(unsigned v)
    {
      printf("x() = %d\n", v);
    }
    
    int main (void)
    {
      unsigned i = 2;
      unsigned a[] = {10,20,30,40};
    
      printf("your way...\n");
      i++;
      x( a[i-1] );
      i--;
    
      printf("\nmy way...\n");
      x( a[i] );
    
      printf("\nqed\n");
    
      return 0;
    }
    DavT
    -----------------------------------------------

  7. #7
    Registered User
    Join Date
    Jun 2003
    Posts
    4
    Yes, exactly, i want to have that side effect, because i use it in a special environment, where i must save wherever i can. I have less then 1k for variables...

    otherwise i could make it this way:

    x(a[i],++i);
    i--;

    or if I declare i as local
    x(a[i],i+1);

Popular pages Recent additions subscribe to a feed