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 5 of 7.

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.

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.

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

Gouri said:   1 decade ago
L value means what?

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

Shri said:   1 decade ago
When we store any value in variable, then c stores it in its internel data structure which is maintained for every program.

To store an variable to things are maintains 1: its address(l value) and 2: its value(r value).

In here a++ is incrementing something c (l value) doesn't know.

Rupinderjit said:   1 decade ago
"a" in function printf acts as an pointer to first element.But when we increment the base address,that is,a++,it gives L value error because we can't increment the subscript of an array in this manner,either we need array indexing plus for loop or we can use pointer arithmetic.

Kiran said:   1 decade ago
Can any one explain what exactly lvalue and rvalue means ?im not getting them?

Kiran said:   1 decade ago
From the above explination in order to rectify the error we use a pointer variable which points to an array.

int main()
{
int a[] = {10, 20, 30, 40, 50};
int j,*ptr;
ptr=&a;
for(j=0; j<5; j++)
{
printf("%d\n", *ptr);
ptr++;
}
return 0;
}

Prakash said:   1 decade ago
int main()
{
int a[] = {10, 20, 30, 40, 50};
int j;
for(j=0; j<5; j++)
{
printf("%d\n", a[j]);

}
return 0;
}
compile this you all can note down the difference ..Thank You


Post your comments here:

Your comments will be displayed after verification.