1

Possible Duplicate:
Is array name a pointer in C?

Suppose I have a char array say arr and arr will represent the address of first element so arr++ should be perfectly legal then why compiler says 'lvalue required'.

Also if I do: arr=arr+1 then why it is invalid conversion. I am just increasing pointer by one. Compiler tells that on LHS the operand type is char[4] but on RHS it is char *.

main()
{
char arr[]={'a','b','c','d'};
for(;arr!=arr+4;arr++) //lvalue required 
printf("%c",*arr);
}
2
  • post your code what you've tried so far Commented Dec 2, 2012 at 7:11
  • 1
    "arr will represent the address of first element" No, it will represent an array. That array may be implicitly converted to a pointer, which will have the same value as &arr[0], but that doesn't make it a pointer. Commented Dec 2, 2012 at 7:27

3 Answers 3

6

An array name is not a variable that can be assigned to. If you want to modify it, you should use a pointer variable:

char *arr_ptr = arr;

You can then do arr_ptr++ and arr_ptr = arr_ptr+1;

Sign up to request clarification or add additional context in comments.

Comments

2

Arrays aren't pointers. arr does not represent an address.

4 Comments

arr does represent of fist element address.
@GrijeshChauhan No, it doesn't (and that was barely coherent).
What I mean arr contains fist element's address..Although I am not down-voter!
arr doesn't contain an address.
2

An array name, or any expression of array type, is implicitly converted to a pointer to the array's first element unless it's either:

  • The operand of a unary & (address-of) expression (which yields the address of the whole array, not of its first element -- same memory address, different type); or
  • The operand of a sizeof operator (sizeof arr yields the size of the array, not the size of a pointer); or
  • The operand of an _Alignof operator (_Alignof arr yields the alignment of the array, not the alignment of a pointer); or
  • A string literal in an initializer that's used to initialize an arrary object.

_Alignof is new in C2011.

(The conversion is often referred to as a "decaying".)

None of these apply here, so arr++ tries to increment a pointer object. The problem is, there is no pointer object, just a pointer value. arr, after it decays to a pointer value, is not an lvalue, which means it cannot appear on the left side of an assignment or as the operand of ++.

Similarly, given:

int x;

the expression x is an lvalue, so you can write x = 42; -- but the expression (x + 1) is not an lvalue, so you can't write (x + 1) = 42;. You can assign to x because it refers to an object. (x+1) doesn't refer to an object, so there's nothing to assign to. arr, after it decays, doesn't refer to an object either (you have an array object, but there's no pointer object).

(Your use of arr != arr + 4 should have been a clue; that can never be true.)

Rather than this:

char arr[] = {'a', 'b', 'c', 'd'};
for (; arr != arr + 4; arr++) {
    printf("%c", *arr);
}

you can write this:

char arr[] = {'a', 'b', 'c', 'd'};
char *ptr;
for (ptr = arr; ptr != arr + 4; ptr++) {
    printf("%c", &ptr);
}

Incidentally, at the top of your program, you should change this:

main()

to this:

#include <stdio.h>
int main(void)

Also, run, do not walk, to the comp.lang.c FAQ and read section 6, "Arrays and pointers".

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.