incrementing an array as formal arg

This is a discussion on incrementing an array as formal arg within the C++ Programming forums, part of the General Programming Boards category; We are not allowed to pre and post increment any array like Code: int array[10]; array++;//Illegal because base address of ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    incrementing an array as formal arg

    We are not allowed to pre and post increment any array like
    Code:
    int array[10];
    array++;//Illegal because base address of an array cannot be changed.
    But in a function(if it is formal argument why it is legal?
    Code:
    void function(char *array)
    {
    array++;//Legal
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Because an array is not a pointer and a pointer is not an array. And a modifiable copy of a location is not the same as a nonmodifiable location.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Compare
    Code:
    3++; // illegal
    With
    Code:
    func(3);
    
    ...
    
    void func ( int a ) {
      a++; // legal - changes the value stored in a, not the value of 3
    }
    Function arguments are variables containing copies of the original data.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    I think my question is not clear to you.So I am repeating
    Code:
    function(char *a[])
    {
    char *b[3];
    b++;//Error
    a++;//Legal Why????
    }
    Or more precisely how the Lvalue is comming in formal argument.

  5. #5
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Code:
    char *b[3];
    b is an array of 3 elements of character pointers. If you use 'b' you really mean
    the address of the first element (i.e. &b[0]). When you attempt to use b++,
    you are telling the compiler to change the address of the constant address of the
    array which is called 'b' in this case. Just like you said in your opening post. Thus
    this is illegal since you are trying to change a constant address. 'b' itself also is
    only an rvalue I believe. So you can't assign/change it in anyway.

    Code:
    function(char *a[])
    ...
    a++;
    Is "legal" because "arrays" get represented as pointers when passed as parameters.
    i.e. 'a' in this case becomes a pointer to the first element. It is legal to apply
    pointer arithmetic on non-constant pointers - thus this statement is legal.

    Not sure if I explained it correct though, do correct me if it's wrong
    The cost of software maintenance increases with the square of the programmer's creativity.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by vaibhav
    I think my question is not clear to you.So I am repeating
    Code:
    function(char *a[])
    {
    char *b[3];
    b++;//Error
    a++;//Legal Why????
    }
    I think my answer is not clear to you. So I am repeating.

    Here
    Code:
    function(char *a[])
    a is not an array. It is a pointer to a pointer to char.

    Here
    Code:
    char *b[3];
    b is an array (of pointers to char).

    You can't increment an array, you can increment a pointer.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. array of pointers/pointer arithmetic
    By tlpog in forum C Programming
    Replies: 18
    Last Post: 11-09-2008, 06:14 PM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 07:01 PM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21