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

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;
}

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.

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.

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
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?

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


Post your comments here:

Your comments will be displayed after verification.