C Programming - Pointers - Discussion

Discussion Forum : Pointers - Point Out Errors (Q.No. 2)
2.
Point out the error in the program
#include<stdio.h>

int main()
{
    int a[] = {10, 20, 30, 40, 50};
    int j;
    for(j=0; j<5; j++)
    {
        printf("%d\n", a);
        a++;
    }
    return 0;
}
Error: Declaration syntax
Error: Expression syntax
Error: LValue required
Error: Rvalue required
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
65 comments Page 3 of 7.

Rajendra Patil said:   1 decade ago
Expressions that refer to memory locations are called "l-value" expressions.
The term "r-value" is sometimes used to describe the value of an expression

Gouri said:   1 decade ago
L value means what?

Gouri said:   1 decade ago
Why L value is required? Please can any one explain me, I can't understand this question.

Surender said:   1 decade ago
@Gouri

L Value means Location Value ( Addressable memory ).
Here a[], a is constant pointer, pointing to the base address of a array 'a'.
we can't update constant after assignment.

So here L-value required means Our Program asking for such a memory location where he can store value of 'a' after the increment (a++).

Hope you got your answer.

Hemlata said:   1 decade ago
I agree with sam, here a is a simple variable not a array. If we want to print a[] a as array we have to increment only j. That is a[j]...there is no meaning of incrementing "a" variable....thats why error is coming.

@Surender

Thanx for giving above information.

Neha said:   1 decade ago
The error is because array is a constant pointer and we can't increment or decrement its base address, we can do it by assigning it into some pointer variable. Thats why the error is lvalue error as we are not providing left side pointer for assignment.

RKV Gopi said:   1 decade ago
yes I agree with Neha..

Veeraselvi said:   1 decade ago
What you mean by Lvalue & Rvalue?

Praveen said:   1 decade ago
Array name itself act as a pointer and it is a constant pointer. So it can not be modified even if you apply a Lvalue to it still it would give you the same error. So you can't modify constant pointer.

You can write this way
printf("%d",(a+1)); inside for loop. It will print the addresses if you want values to be printed then write *(a+1) instead of (a+1).

Kunal Bansal said:   1 decade ago
It's simple , thing is you can do arithmetic operation like ++,-- and others on pointers but these operation are not allowed while implementing on array .
Reason : pointer is a variable
array name is not a variable


Post your comments here:

Your comments will be displayed after verification.