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

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.

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.

Aswini said:   1 decade ago
The reference to the variable a refers to the base address of array a[].In general the statement a++ increments and assigns the value back to a.But here this statement affects the base address of an array while incrementing and assigning it to a......this what i understood

Mukul garg said:   1 decade ago
The only thing which provide functionality of random access is contiguous memory allocation and array has only its base address to access all elements of the array if we modify this then array can not be able to know the starting address so this gives error.

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

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.

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

Simply a constant pointer can not be modified.

Tim said:   1 decade ago
The a++ generates "non l-value in assignment" in GNU G++ and "wrong type argument to increment" in GNU C compiler. Your test should be about the C LANGUAGE, not about specific error messages in the C# compiler.

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.