Thread: Syntax question: &(++x)

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    5

    Syntax question: &(++x)

    Hi, just a weird syntax-related question. Why can't I do this:

    Code:
    some_function( &(++x) );
    where its passing a pointer to an already-incremented variable x? Seems to me it should be legal, but gcc complains about invalid lvalues. Any switches I can pass to make this code work? Cheers,
    Yoshi


    EDIT: In other words, why isn't ++x an lvalue? Two lines for one thing is so ugly :-)
    Last edited by yoshiznit123; 06-01-2006 at 06:15 PM.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Honestly, this looks like the address of a pointer. The correct answer depends on what you are doing. If you want to increment the *value at* the pointer and pass it in, you are no longer passing a pointer, but if you had a data structure and need to pass in a certain part of it, you'd just increment *the pointer*.


    Code:
       // my favorite function f...
       void f(int * in) {
    
       }
    ...
       int x[] = {42, 1, 0, -42};
       f( (x++) ); // make sure that the pointer is incremented, 
                   // so we pass x[1]
    Last edited by whiteflags; 06-01-2006 at 06:39 PM.

  3. #3
    Registered User
    Join Date
    Jun 2006
    Posts
    5
    Yea, I guess that would work. The reason I had it is this:

    Code:
    void skip_space(char** str)
    {
    	for(; **str != '\0' && isspace(**str); (*str)++);
    }
    which takes a pointer to a pointer to a string and slides it over until it points to the first nonspace. I wanted something like this:

    Code:
    char x[50] = "#    Some comment with leading spaces...";
    char* y = x;
    
    skip_space( &(++x) );
    to skip the first '#' and then skip spaces, but then I realized there were much better ways. Thanks anywas :-). Cheers,
    Yoshi

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by yoshiznit123
    Yea, I guess that would work. The reason I had it is this:

    Code:
    void skip_space(char** str)
    {
    	for(; **str != '\0' && isspace(**str); (*str)++);
    }
    which takes a pointer to a pointer to a string and slides it over until it points to the first nonspace. I wanted something like this:

    Code:
    char x[50] = "#    Some comment with leading spaces...";
    char* y = x;
    
    skip_space( &(++x) );
    to skip the first '#' and then skip spaces, but then I realized there were much better ways. Thanks anywas :-). Cheers,
    Yoshi
    Your problem there is that you are trying to increment an array. You cannot do this. What you can do is &(x+1), becouse then x stays the same.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User OnionKnight's Avatar
    Join Date
    Jan 2005
    Posts
    555
    Any form of arithmetic will degrade LValues to RValues. You have to do it on two lines.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by OnionKnight
    Any form of arithmetic will degrade LValues to RValues. You have to do it on two lines.
    No you don't. You simmply cannot use an array as an LValue in asignment.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by King Mir
    No you don't. You simmply cannot use an array as an LValue in asignment.
    You're on crack, Mir:
    Code:
    itsme@itsme:~/C$ cat lvartest.c
    #include <stdio.h>
    
    void print_it(char **str)
    {
      puts(*str);
    }
    
    int main(void)
    {
      char str[] = "Hello world!";
      char *pstr = str;
    
      print_it(&(++pstr));
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ gcc -Wall lvartest.c -o lvartest
    lvartest.c: In function `main':
    lvartest.c:13: error: invalid lvalue in unary `&'
    itsme@itsme:~/C$
    OnionKnight is correct.

    Take the ++ out of print_it(&(++pstr)); and it compiles and runs just fine. And don't try to tell me it's because pstr points to an array. I'm trying to increment the pointer, not the array.

    You also get the same error with print_it(&(pstr + 1));.
    Last edited by itsme86; 06-02-2006 at 12:16 PM.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    I think King Mir's point is that the OP's code will not work, even if split on two lines, as an array is not an l-value and therefore the ++ and other assignment operators can not be used on it.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I don't know why they bother doing anything with x at all, since they already have a pointer to it with y. Increment that. You still have to split it on two lines however.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using c++ in c code
    By hannibar in forum C Programming
    Replies: 17
    Last Post: 10-28-2005, 09:09 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  5. Question on function syntax and calling function
    By cbrman in forum C Programming
    Replies: 10
    Last Post: 10-05-2003, 05:32 PM