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

Manoj S said:   5 months ago
The array name is also a pointer.
But it is a constant pointer, which means it can't be modified. So, we need an LValue(Left-hand value) to make it possible for traversal.
i.e: int *ptr = a;
(1)

Rakshitha said:   4 years ago
Explain the answer clearly.
(1)

Kausa chan said:   4 years ago
No, @Kiran.

Because ptr is just pointer to an integer and you're storing the address of an array to it so it will throw an error.
(1)

Nani said:   5 years ago
We can't do. a=a+1;

Because base address will change. This is illegal. If a is pointer we can do it.
(2)

Traian said:   5 years ago
L - Value means an object that has an identifiable location in the memory(has an address) Ex: var a;
R - Value means an object with no identifiable location in the memory Ex: (a+b);
(6)

Siddharth_dixit said:   6 years ago
#include<stdio.h>

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

Vallinayagam said:   6 years ago
In an array, it is not possible to perform increment/decrement by using the array name. If we want we can use a pointer.

Shweta said:   6 years ago
Array is a constant pointer so we can't increment or decrement an array.

Parth said:   7 years ago
The reason why Dennis didn't allowed us to play with the name of the array was b/c it is the only way to access the array we have declared. Ones the base address of the array is lost we can't access it. It still exists in the memory but it's hidden in a pool of 1073741824 bytes of RAM memory.(If you are running on 1GB ).
(2)

Coder x said:   7 years ago
You cannot increment and decrement operations on the array directly. You need a pointer to be able to access the values of the pointer.


Post your comments here:

Your comments will be displayed after verification.