Thread: Lvalue pointer

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Lvalue pointer


    char a[]="hello";
    a++;.
    gives error as lvalue required
    but no error if "a" is char pointer..what is the explaination for this??
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep Power
    34
    Pointers are not arrays and arrays are not pointers.
    pointers are lvalues; arrays are not lvalues.

    You might like to read section 6 of the comp.lang.c FAQ.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    71
    Rep Power
    2
    In your example 'a' is fixed and constant, you can't modify its value. It would be like changing the address of this example

    Code:
    int a = 9;
    (&a)++;
    Which generates this error on my system - error: lvalue required as increment operand.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    2

    Array is a constant pointer


    Array is a constant pointer. Pointer always holds the address of a variable. Constant pointer means you cannot change the address where pointer is pointing to.

    Because of this you cannot modify the base address of the array. a++ is trying to increment the base address of array, which is not possible.
  8. #5
  9. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    Arrays are NOT constant pointers.
    Didn't you read bdb's post?
    CLC FAQ

    Eg, given
    char arr[10];

    It isn't a pointer (case 1), because sizeof(a) tells you how many bytes are allocated to the array. sizeof() on a pointer gives you (mostly) the same answer on any given machine, regardless of how much memory it points to.

    It isn't a pointer (case 2), because the type of &arr isn't char** (which is would be if you had char *arr). The type of &arr is char(*)[10]

    It isn't a pointer (case 3), because you can initialise an array, but you can't initialise a pointer in the same way.
    // allocates space for 3 ints and initialises to 1,2,3
    int a[] = { 1,2,3 };
    // does not allocate space for 3 int's and sets b to point to the start of 1,2,3
    int *b = { 1,2,3 };


    That arrays "decay into pointers to the first element of the array" in most cases does not make arrays themselves pointers.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    30
    Rep Power
    2
    Originally Posted by salem
    Arrays are NOT constant pointers.
    Didn't you read bdb's post?
    CLC FAQ

    Eg, given
    char arr[10];

    It isn't a pointer (case 1), because sizeof(a) tells you how many bytes are allocated to the array. sizeof() on a pointer gives you (mostly) the same answer on any given machine, regardless of how much memory it points to.

    It isn't a pointer (case 2), because the type of &arr isn't char** (which is would be if you had char *arr). The type of &arr is char(*)[10]

    It isn't a pointer (case 3), because you can initialise an array, but you can't initialise a pointer in the same way.
    // allocates space for 3 ints and initialises to 1,2,3
    int a[] = { 1,2,3 };
    // does not allocate space for 3 int's and sets b to point to the start of 1,2,3
    int *b = { 1,2,3 };


    That arrays "decay into pointers to the first element of the array" in most cases does not make arrays themselves pointers.
    Yes I agree. In terms of base address of array what I have mentioned holds good.

IMN logo majestic logo threadwatch logo seochat tools logo