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.

Jana said:   10 years ago
Friends, whether we can assign a pointer to a pointer.

Eg:
{
*b=*a;
}

Balaji said:   1 decade ago
I am not understand explain clearly.

Aditya reja said:   1 decade ago
The name of An Array is automatically constant pointer (i.e. cannot be alter) that always points to its 0th element. Due to its constant nature ++ or -- operators can't be apply to array's name.

Aim said:   1 decade ago
Here printf statement display address of memory location so when we do a++ is increment the address value but here left side any variable is not available to take it. So error is LValue required

Nila said:   1 decade ago
@Bele.

We use the array's variable name(without subscript )for sake of passing them to a function which receives with a pointer variable.

So now that pointer will be holding the base address of the array..using which you could access the rest of the array elements(coz array elements are continuously stored).

This reduces the degrading of performance like stack overflow in passing arrays on the whole to a function.

Bele said:   1 decade ago
What is the reason to make array names as cons pointer, and give them the address of the first element, if we give them the address of the first element why we can't modify them and get the other elements? is there security issue or performance or something else. By not making arrays names const ptr?

Atul yadav said:   1 decade ago
Simply replace a++; with a[j]++;

Anil said:   1 decade ago
Arrays internally follows const pointer mechanism.

ex: int const *arr =(int*)malloc(10*sizeof(int));

Can you perform increment operation here. No never not possible.

int *arr =(int*)malloc(10*sizeof(int));

Here increment operation works.

So its not matter of L-value. We can't change array base address that's the answer. Because array internally for const pointer mechanism.

Chandru said:   1 decade ago
@Sudha, can you please explain me with example program?

POOJA said:   1 decade ago
Very simple logic.

a is an array.
We can't perform following operations on array:

a++;
a--;
a+=2;
....

If pointer to an array will be there then we can perform above operations.


Post your comments here:

Your comments will be displayed after verification.